﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Telerik.Web.UI;
using KPIS.GERP.ESS.APP.App_UI.Commons;
using KPIS.GERP.ESS.BLL;
using KPIS.GERP.ESS.DAL;
using System.Linq;
using System.Web.UI;
using System.Text;
//Adding
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.IBLL;
using KPIS.GERP.GFMS.BLL;
using KPIS.DBM;

namespace KPIS.GERP.ESS.APP.App_UI.Forms
{
    public partial class ESS_MoneyRequest : System.Web.UI.Page
    {

        #region Fields

        Main main = new Main();
        MailMerge mailMerge = new MailMerge();
        BGM_ProjectOperation.BGM_ProjectOperation po = new BGM_ProjectOperation.BGM_ProjectOperation();
        BGM_ProjectOperationExpense.BGM_ProjectOperationExpense poexp = new BGM_ProjectOperationExpense.BGM_ProjectOperationExpense();
        BGM_ProjectOperationExpenseRate.BGM_ProjectOperationExpenseRate poexprate = new BGM_ProjectOperationExpenseRate.BGM_ProjectOperationExpenseRate();
        BGM_ProjectOperationPerson.BGM_ProjectOperationPerson poper = new BGM_ProjectOperationPerson.BGM_ProjectOperationPerson();
        BGM_ProjectOperationReport.BGM_ProjectOperationReport porpt = new BGM_ProjectOperationReport.BGM_ProjectOperationReport();
        //BGM_MAS_Option.BGM_MAS_Option op = new BGM_MAS_Option.BGM_MAS_Option();
        BGM_MAS_Source.BGM_MAS_Source source = new BGM_MAS_Source.BGM_MAS_Source();
        BGM_MAS_ExpenseGroup.BGM_MAS_ExpenseGroup expGrp = new BGM_MAS_ExpenseGroup.BGM_MAS_ExpenseGroup();
        FIM_Employees.FIM_Employees emp = new FIM_Employees.FIM_Employees();
        D_HrmJobsites jobsite = new D_HrmJobsites();

        FIM_MoneyRequests.FIM_MoneyRequests req = new FIM_MoneyRequests.FIM_MoneyRequests();
        FIM_MoneyRequestDtls.FIM_MoneyRequestDtls reqDtls = new FIM_MoneyRequestDtls.FIM_MoneyRequestDtls();
        FIM_MoneyRequestTravelers.FIM_MoneyRequestTravelers reqTrav = new FIM_MoneyRequestTravelers.FIM_MoneyRequestTravelers();
        FIM_MoneyRequestDtlsRates.FIM_MoneyRequestDtlsRates reqRate = new FIM_MoneyRequestDtlsRates.FIM_MoneyRequestDtlsRates();
        FIM_MAS_ExpenseRates.FIM_MAS_ExpenseRates exprate = new FIM_MAS_ExpenseRates.FIM_MAS_ExpenseRates();
        //FIM_MAS_ExpenseRateTypes.FIM_MAS_ExpenseRateTypes rateType = new FIM_MAS_ExpenseRateTypes.FIM_MAS_ExpenseRateTypes();
        //FIM_MAS_ExpenseRateTypeGroups.FIM_MAS_ExpenseRateTypeGroups rateTypeGrp = new FIM_MAS_ExpenseRateTypeGroups.FIM_MAS_ExpenseRateTypeGroups();
        FIM_MAS_BorrowTypes.FIM_MAS_BorrowTypes borrowType = new FIM_MAS_BorrowTypes.FIM_MAS_BorrowTypes();
        //FIM_MAS_Currencies.FIM_MAS_Currencies currency = new FIM_MAS_Currencies.FIM_MAS_Currencies();
        FIM_MAS_Contacts.FIM_MAS_Contacts contact = new FIM_MAS_Contacts.FIM_MAS_Contacts();
        B_HrmEmployees emplist = new B_HrmEmployees();
        B_IcmMasResourceItem icmItem = new B_IcmMasResourceItem();
        ESS.BLL.B_MessageSend send = new ESS.BLL.B_MessageSend();
        ESS.BLL.B_MessageReply reply = new ESS.BLL.B_MessageReply();
        D_FimMoneyRequest req2 = new D_FimMoneyRequest();
        DBManager dbManager = new DBManager(DataProvider.SqlServer, SystemInfo._ConnectionString);

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            RegisClientScript();

            if (!IsPostBack)
            {
                // main code
                SetPagePermission();
                cmdProgress1_Click(null, null);
                SetProgress(null);

                // optional code
                LoadInfo();
                LoadDefault();
                LoadDDLBgmSourceSeq();
                LoadRblBorrowTypeSeq();
                LoadDDLBgmProjOperSeq();
                //LoadDDLBgmProjOperSeq1();
                LoadRblBgmExpGrpSeq();
                //LoadddlBgmExpGrpSeq();
                LoadDDLJobSiteSeq();
                LoadDDLContactSeq();
                LoadData();
                rblEventSeq_SelectedIndexChanged(null, null);
            }
        }

        protected void cmdAddNew1_Click(object sender, EventArgs e)
        {
            if (liViewAll.Attributes["class"] == "Selected")
            {
                lblMoneyReqSeq.Text = "";
                SetProgress("");
                ClearControl1();
            }

            SetBody("PanelProgress1");
            SetControl("AddNew");
        }

        protected void cmdInsertMuti_Click(object sender, EventArgs e)
        {
            try
            {
                InsertNew();
            }
            catch (Exception ex)
            {

                RadAjaxManager manager = RadAjaxManager.GetCurrent(Page);
                manager.Alert(ex.Message);
            }
        }

        protected void cmdInsert1_Click(object sender, EventArgs e)
        {
            try
            {
                if (PanelProgress1.Visible)
                {
                    //Insert1();
                    InsertNew();
                }
                /*
                else if (PanelProgress2.Visible)
                {
                    Insert2();
                }
                */
                else if (PanelProgress3.Visible)
                {
                    Insert3();
                }
            }
            catch (Exception ex)
            {

                RadAjaxManager manager = RadAjaxManager.GetCurrent(Page);
                manager.Alert(ex.Message);
            }
        }

        protected void cmdUpdate1_Click(object sender, EventArgs e)
        {
            if (PanelProgress1.Visible)
            {
                Update1();
            }

            /*
            else if (PanelProgress2.Visible)
            {
                Update2();
            }
            */

            else if (PanelProgress3.Visible)
            {
                Update3();
            }
        }

        protected void cmdEdit1_Click(object sender, EventArgs e)
        {

            //Modified by Raksakaew on Feb 26,2014 add if clause to check datagridview selected index
            if (GrdViewAll.SelectedIndexes.Count > 0)
            {
                GridDataItem item = GrdViewAll.Items[GrdViewAll.SelectedIndexes[0]];
                string strMoneyReqSeq = main.WebString(item["money_req_seq"].Text);
                Session["ReportSeq"] = strMoneyReqSeq;

                FIM_MoneyRequests.M_FIM_MoneyRequests info = req.GetInfo(strMoneyReqSeq);

                lblMoneyReqSeq.Text = strMoneyReqSeq;
                lblMoneyReqDate.Text = info.money_req_date.Value.ToShortDateString();
                txtMoneyReqDate.SetValue(info.money_req_date);
                txtBgmYear.Text = info.bgm_year.ToString();
                ddlBgmSourceSeq.SelectedValue = info.bgm_source_seq.ToString();
                ddlBgmSourceSeq_SelectedIndexChanged(null, null);
                lblRefNo.Text = info.ref_no.ToString();
                ddlBgmProjOperSeq.SelectedValue = info.bgm_proj_oper_seq.ToString();
                txtMoneyReqName.Text = info.money_req_name;
                lblRecordStatus.Text = info.record_status;
                lblFinRecordStatus.Text = info.record_status;
                lblFinRecordStatusName.Text = info.record_status_name;
                rblEventSeq.SelectedValue = info.event_seq.ToString();
                rblEventSeq_SelectedIndexChanged(null, null);
                rblBgmExpGrpSeq.SelectedValue = (info.bgm_exp_grp_seq != null ? info.bgm_exp_grp_seq.ToString() : "2");

                lblMoneyReqName.Text = info.money_req_name;
                lblMoneyReqName2.Text = info.money_req_name;

                if (rblEventSeq.SelectedValue == "16" || rblEventSeq.SelectedValue == "18")
                {
                    rblBorrowTypeSeq.SelectedValue = info.borrow_type_seq.ToString();
                    rblBorrowTypeSeq_SelectedIndexChanged(null, null);

                    dpBorrowStartDate.SetValue(info.borrow_start_date);
                    dpBorrowEndDate.SetValue(info.borrow_end_date);
                    lblBorrowReturnDate.Text = main.ConvertDBDateTimeToString(info.borrow_return_date, false);

                    if (li3.Visible)
                    {
                        LoadRepeaterPerson();
                    }
                }
                else if (rblEventSeq.SelectedValue == "19")
                {
                    rblTransferTypeSeq.SelectedValue = info.transfer_type_seq.ToString();
                    rblTransferTypeSeq_SelectedIndexChanged(null, null);

                    ddlJobSiteSeq.SelectedValue = info.transfer_job_site_seq.ToString();
                    dpBorrowStartDate2.SetValue(info.borrow_start_date);
                    dpBorrowEndDate2.SetValue(info.borrow_end_date);
                    lblBorrowReturnDate2.Text = main.ConvertDBDateTimeToString(info.borrow_return_date, false);

                    if (li3.Visible)
                    {
                        LoadRepeaterPerson();
                    }
                }
                else if (rblEventSeq.SelectedValue == "12")
                {
                    rblPayeeType.SelectedValue = info.contact_seq.HasValue ? "2" : "1";
                    rblPayeeType_SelectedIndexChanged(null, null);

                    if (info.contact_seq.HasValue)
                    {
                        ddlContactSeq.SelectedValue = info.contact_seq.ToString();
                    }
                }

                //LoadRepeater1();
                SetProgress(strMoneyReqSeq);
                cmdSummary_Click(null, null);
                SetBody("PanelSummary");
                rblBgmExpGrpSeq.Enabled = (info.bgm_exp_grp_seq == null);
                ddlJobSiteSeq.Enabled = false;
            }

        }

        protected void cmdCancel_Click(object sender, EventArgs e)
        {
            SetControl("AddNew");

            if (PanelProgress1.Visible)
            {
                ClearControl1();
                SetProgress(null);
                lblMoneyReqSeq.Text = "";
            }
            else if (PanelProgress2.Visible)
            {
                ClearControl2();
            }
            else if (PanelProgress3.Visible)
            {
                ClearControl3();
            }
            else if (PanelSummary.Visible)
            {
                cmdViewAll_Click(null, null);
                SetProgress(null);
            }
        }

        protected void cmdDelete1_Click(object sender, EventArgs e)
        {
            if (PanelProgress1.Visible)
            {
                Delete1();
            }

            /*
            if (PanelProgress2.Visible)
            {
                Delete2();
            }
            */

            if (PanelProgress3.Visible)
            {
                Delete3();
            }

            if (PanelViewAll.Visible)
            {
                Delete1();
            }
        }

        protected void cmdProgress1_Click(object sender, EventArgs e)
        {
            SetBody("PanelProgress1");

            if (imgInComplete1.Visible)
            {
                ddlBgmProjOperSeq.Enabled = rblEventSeq.SelectedValue != "17";

                SetControl("AddNew");
                lblRecordStatus.Text = "W";
                lblFinRecordStatus.Text = "W";
                lblFinRecordStatusName.Text = "รอการยืนยัน";
            }
            else
            {
                ddlBgmProjOperSeq.Enabled = false;
                SetControl("Edit");
            }
        }

        protected void cmdProgress2_Click(object sender, EventArgs e)
        {
            //LoadRepeater4();
            LoadGrdMoneyRequestDtls();
            SetBody("PanelProgress2");
            SetControl("NoControl");

            if (imgInComplete2.Visible)
            {
                rblBgmExpGrpSeq.Enabled = true;
            }
            else
            {
                rblBgmExpGrpSeq.Enabled = false;
            }
        }

        protected void cmdProgress3_Click(object sender, EventArgs e)
        {
            LoadRepeaterPerson();
            SetBody("PanelProgress3");
            SetControl("AddNew");
        }

        protected void cmdSummary_Click(object sender, EventArgs e)
        {
            SetBody("PanelSummary");
            SetControl("NoControl");
            LoadFinData();
            PanelMessageSend.Visible = false;
        }

        protected void cmdViewAll_Click(object sender, EventArgs e)
        {
            LoadGrdViewAll();
            SetBody("PanelViewAll");
            SetControl("ViewAll");
        }

        /*
        protected void cmdCloseExpenseSummary_Click(object sender, ImageClickEventArgs e)
        {
            PanelExpenseSummary.Visible = false;
        }

        protected void cmdOpenExpenseSummary_Click(object sender, EventArgs e)
        {
            PanelExpenseSummary.Visible = true;
        }
        */

        protected void ddlBgmProjOperSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            //LoadRepeater1();
            string strBgmProjOperName = ddlBgmProjOperSeq.Text;

            if (ddlBgmProjOperSeq.SelectedIndex > 0)
            {
                txtMoneyReqName.Text = strBgmProjOperName;
                lblMoneyReqName.Text = strBgmProjOperName;
                lblMoneyReqName2.Text = strBgmProjOperName;
            }
            else
            {
                txtMoneyReqName.Text = "";
                lblMoneyReqName.Text = "";
                lblMoneyReqName2.Text = "";
            }
        }

        //protected void ddlBgmProjOperSeq1_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        //{
        //    string strBgmProjOperName1 = ddlBgmProjOperSeq1.Text;

        //    if (ddlBgmProjOperSeq1.SelectedIndex > 0)
        //    {
        //        grdMoneyRequestDtlsNew.Visible = true;
        //        ddlBgmExpGrpSeqNew.Enabled = true;
        //        //btAddBgmExpGrpSeqNew.Enabled = true;
        //        //PanelPayeeType.Visible = true;
        //        txtMoneyReqName1.Text = strBgmProjOperName1;
        //        lblMoneyReqName.Text = strBgmProjOperName1;
        //        lblMoneyReqName2.Text = strBgmProjOperName1;
        //        //GrdAddConAndMoney.MasterTableView.Columns.Clear();
        //    }
        //    else
        //    {
        //        txtMoneyReqName1.Text = "";
        //        lblMoneyReqName.Text = "";
        //        lblMoneyReqName2.Text = "";
        //    }

        //    LoadGrdMoneyRequestDtlsNew();
        //}

        protected void grdMoneyRequestDtls_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "AddExpense")
            {
                GridDataItem item = (GridDataItem)e.Item;
                DataTable dt = new DataTable();
                D_FimMoneyRequest fm = new D_FimMoneyRequest();
                lblMoneyReqDtlsSeq.Text = main.WebString(item["money_req_dtls_seq"].Text);
                lblBgmProjOperExpSeq.Text = main.WebString(item["m_bgm_projectoperationexpense.bgm_proj_oper_exp_seq"].Text);
                lblBgmExpName.Text = main.WebString(item["m_bgm_projectoperationexpense.m_bgm_projectexpense.m_bgm_mas_expense.bgm_exp_name"].Text);
                //lblRemainAmount.Text = main.WebString(item["m_bgm_projectoperationexpense.remain_amount"].Text);
                lblRemainAmount.Text = main.WebString(item["m_bgm_projectoperationexpense.remain_amount"].Text);
                lblBgmExpSeq.Text = main.WebString(item["m_bgm_projectoperationexpense.m_bgm_projectexpense.bgm_exp_seq"].Text);
                ddlVender.SelectedValue = main.WebString(item["ConId"].Text);
                txtBranchNo.Text = main.WebString(item["branch_no"].Text);
                txtDtlsRefNo.Text = main.WebString(item["ref_no"].Text);
                txtDtlsRefDate.SetValue(main.WebString(item["ref_date"].Text));

                double dAmount = main.ConvertNullDouble(main.WebString(item["amount"].Text)) ?? 0;
                double dPercentVat = main.ConvertNullDouble(main.WebString(item["percent_vat"].Text)) ?? 0;
                double dPercentTax = main.ConvertNullDouble(main.WebString(item["percent_tax"].Text)) ?? 0;
                double dAmountVat = main.ConvertNullDouble(main.WebString(item["amount_vat"].Text)) ?? 0;
                double dAmountTax = main.ConvertNullDouble(main.WebString(item["amount_tax"].Text)) ?? 0;
                double dExcludeVat = dAmount - dAmountVat;

                chkIncludeVat.Checked = dPercentVat > 0;
                chkIncludeTax.Checked = dPercentTax > 0;

                panelVat.Visible = chkIncludeVat.Checked;
                panelTax.Visible = chkIncludeTax.Checked;
                panelTotalAmount.Visible = chkIncludeVat.Checked || chkIncludeTax.Checked;

                txtAmount.Text = dAmount.ToString("#,##0.00");
                txtPercentVat.Text = dPercentVat.ToString("#,##0.00");
                txtPercentTax.Text = dPercentTax.ToString("#,##0.00");
                txtAmountVat.Text = dAmountVat.ToString("#,##0.00");
                txtAmountTax.Text = dAmountTax.ToString("#,##0.00");
                lblExcludeVat.Text = dExcludeVat.ToString("#,##0.00");
                lblTotalAmount.Text = dAmount.ToString("#,##0.00");

                lblProjOperRemainAmount.Text = ((main.ConvertNullDouble(lblProjOperRemainAmount.Text) ?? 0)
                    + (main.ConvertNullDouble(txtAmount.Text) ?? 0)).ToString("#,##0.00");
                lblProjOperRemainAmount2.Text = ((main.ConvertNullDouble(lblProjOperRemainAmount2.Text) ?? 0)
                    + (main.ConvertNullDouble(txtAmount.Text) ?? 0)).ToString("#,##0.00");

                lblBgmSourceSeq.Text = ddlBgmSourceSeq.SelectedValue;
                lblBgmExpGrpSeq.Text = rblBgmExpGrpSeq.SelectedValue;
                txtRemarks.Text = main.WebString(item["remarks"].Text);
                ddlVender.SelectedValue = main.WebString(item["ConId"].Text);

                //lblBathAmount.Text = main.WebString(item["bath_amount"].Text);
                ViewState["Amount"] = main.ConvertNullDouble(txtAmount.Text);
                string strBgmExpGrpSeq = rblBgmExpGrpSeq.SelectedValue;//main.WebString(item["m_bgm_projectoperationexpense.m_bgm_projectexpense.m_bgm_mas_expense.bgm_exp_grp_seq"].Text);
                int nExpRateCount = Convert.ToInt32(main.WebString(item["m_bgm_projectoperationexpense.m_bgm_projectexpense.m_bgm_mas_expense.exp_rate_count"].Text));

                if (strBgmExpGrpSeq == "3")
                {
                    lblIcmResItemSeq.Text = main.WebString(item["m_bgm_projectoperationexpense.icm_res_item_seq"].Text);

                    DataRow dr = icmItem.GetInfo(lblIcmResItemSeq.Text);

                    lblIcmResItemCode.Text = dr["icm_res_item_code"].ToString();
                    lblIcmResItemName.Text = dr["icm_res_item_name"].ToString();

                    lblUnitPriceProj.Text = main.WebString(item["m_bgm_projectoperationexpense.unit_price"].Text);
                    lblUnitQtyProj.Text = main.WebString(item["m_bgm_projectoperationexpense.unit_qty"].Text);
                    txtAmount.Text = main.WebString(item["m_bgm_projectoperationexpense.bath_amount"].Text);
                    PanelInv.Visible = true;
                    PanelExpRate.Visible = false;

                    txtAmount.Enabled = false;
                }
                else if (nExpRateCount > 0)
                {
                    PanelInv.Visible = false;

                    /*
                    SetQty(lblBgmExpSeq.Text);

                    Nullable<int> nClassCount = main.ConvertNullInt(main.WebString(item["m_bgm_projectoperationexpense.m_bgm_projectexpense.m_bgm_mas_expense.class_count"].Text));

                    if (nClassCount != null)
                    {
                        if (nClassCount > 0)
                        {
                            PanelExpRate.Visible = false;
                            PanelExpRateEmp.Visible = true;
                            LoadRepeaterExpRateEmp();
                        }
                        else
                        {
                    PanelExpRate.Visible = true;
                    PanelExpRateEmp.Visible = false;
                    chkManual.Visible = true;
                    LoadRepeaterFilter();
                    LoadRepeaterExpRate();
                        }
                    }
                    */

                    txtAmount.Enabled = false;
                }
                else
                {
                    PanelInv.Visible = false;
                    PanelExpRate.Visible = false;
                    PanelExpRateEmp.Visible = false;
                    txtAmount.Enabled = true;

                }

                bool bAddNew = (lblMoneyReqDtlsSeq.Text.Length == 0);
                cmdInsert2.Visible = bAddNew;
                cmdUpdate2.Visible = !bAddNew;
                PanelProjOperExp.Visible = true;
            }

            lblInsertUid.Text = InsertGuidSessionManager.GenerateNewGuid();
            PanelOpacity.Visible = true;
        }

        //protected void grdMoneyRequestDtlsNew_ItemCommand(object source, GridCommandEventArgs e)
        //{
        //    if (e.Item is GridDataItem && (e.Item.ItemIndex % 2 == 0))
        //    {
        //        ((e.Item as GridDataItem)["GridCheckBoxColumn"].Controls[0] as CheckBox).Enabled = false;
        //    }
        //    LoadGrdMoneyRequestDtlsNew();
        //}

        //protected void GrdAddConAndMoney_ItemCommand(object source, GridCommandEventArgs e)
        //{

        //}

        protected void ddlEmpSeq_SelectedIndexChanged(object o, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            string strEmpSeq = ddlEmpSeq.SelectedValue;

            if (strEmpSeq.Length > 0)
            {
                DataTable dtb = emplist.GetInfo(Convert.ToInt32(strEmpSeq));
                lblPosName.Text = dtb.Rows[0]["exec_pos_name"].ToString();
                lblJobsiteName.Text = dtb.Rows[0]["job_site_name"].ToString();
                lblClassName.Text = dtb.Rows[0]["class_name"].ToString();
                lblPosSeq.Text = dtb.Rows[0]["t_pos_seq"].ToString();
                lblJobsiteSeq.Text = dtb.Rows[0]["working_job_site_seq"].ToString();
                lblClassSeq.Text = dtb.Rows[0]["t_class_seq"].ToString();
                ddlEmpSeq.Text = dtb.Rows[0]["full_name"].ToString();
            }
        }

        /*
        protected void ddlEmpSeq_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            LoadDDLEmpSeq(e);
        }
        */

        protected void RepeaterPerson_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            string strCommandName = e.CommandName;

            if (strCommandName == "edit")
            {
                RepeaterItem item = e.Item;
                lblTravelerSeq.Text = main.WebString(((Label)item.FindControl("lblTravelSeq")).Text);
                ddlEmpSeq.SelectedValue = main.WebString(((Label)item.FindControl("lblRepeaterEmpSeq")).Text);
                ddlEmpSeq.Text = main.WebString(((LinkButton)item.FindControl("cmdRepeaterEdit")).Text);

                ddlEmpSeq_SelectedIndexChanged(null, null);
                SetControl("Edit");
                cmdDelete1.Visible = true;
            }
        }

        /*
        protected void RepeaterFilter_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item is RepeaterItem)
            {
                if (e.Item.ItemType != ListItemType.Header)
                {
                    RepeaterItem dataItem = (RepeaterItem)e.Item;
                    Label lbl = (Label)dataItem.FindControl("lblExpRateTypeGrpSeq");
                    //Label lbl2 = (Label)dataItem.FindControl("lblExpRateMapSeq");
                    RadComboBox ddl = (RadComboBox)dataItem.FindControl("ddlExpRateTypeSeq");

                    ddl.DataSource = rateType.GetList(lbl.Text);
                    ddl.DataBind();

                    main.InsertEmptyDDLAll(ddl);
                    ddl.SelectedIndex = 0;
                }
            }
        }
        */

        protected void cmdReportMoneyReqContracts_Click(object sender, EventArgs e)
        {
            string strMoneyReqSeq = string.Empty;

            if ((lblMoneyReqSeq.Text.Trim() == "") & (GrdViewAll.SelectedIndexes.Count > 0))
            {
                GridDataItem item = GrdViewAll.Items[GrdViewAll.SelectedIndexes[0]];
                strMoneyReqSeq = main.WebString(item["money_req_seq"].Text);
            }
            else if (lblMoneyReqSeq.Text.Trim() != "")
            {
                strMoneyReqSeq = lblMoneyReqSeq.Text;
            }
            else
            {
                strMoneyReqSeq = "0";
            }

            cmdReportMoneyReqContracts.PostBackUrl = string.Format("~/App_UI/Forms/frmGetDocxReport.aspx/?arg1={0}&arg2={1}&arg3={2}", "B", strMoneyReqSeq, "");

            /*
            if (lblMoneyReqSeq.Text.Trim() != "")
            {
                if (!PanelSummary.Visible && rblFilterEventSeq.SelectedValue != "16")
                {
                    main.MessageAlert(Page, "รองรับรายการยิมเงินเฉพาะหน้าจอ [สรุปรายการ] เท่านั้น");
                    return;
                }

                string strMoneyReqSeq = lblMoneyReqSeq.Text;

                DataTable dtbMain = req.Report_Contract(strMoneyReqSeq);
                DataTable dtbSub = req.Report_ContractSub(strMoneyReqSeq);

                Dictionary<string, string> fields = new Dictionary<string, string>();

                if (dtbMain.Rows.Count > 0)
                {
                    DataRow dr = dtbMain.Rows[0];
                    fields.Add("BorrowNo", dr["borrow_no"].ToString());
                    fields.Add("RefNo", dr["ref_no"].ToString());
                    fields.Add("BorrowReturnDate", main.ConvertNullDateTime(dr["borrow_return_date"]) != null
                        ? main.ConvertNullDateTime(dr["borrow_return_date"]).Value.ToShortDateString() : "");
                    fields.Add("DebtorName", dr["debtor_name"].ToString());
                    fields.Add("PositionName", dr["debtor_pos_name"].ToString());
                    fields.Add("Province", dr["debtor_prov_name"].ToString());
                    fields.Add("DebtorJobsiteName", dr["debtor_job_site_name"].ToString());
                    fields.Add("MoneyReqName", dr["money_req_name"].ToString());
                    fields.Add("ReturnDay", dr["return_day"].ToString());
                    fields.Add("CalDateTypeName", dr["cal_date_type_name"].ToString());
                    fields.Add("AllowerName", dr["allower_name"].ToString());
                    fields.Add("Amount", main.ConvertNullDouble(dr["amount"]) != null
                        ? main.ConvertNullDouble(dr["amount"]).Value.ToString("#,##0.00") : "0.00");
                    fields.Add("MoneyTextThai", mailMerge.ThaiBaht(dr["amount"].ToString()));
                }

                Dictionary<string, DataTable> Table = new Dictionary<string, DataTable>();
                Table.Add("table1", dtbSub);

                string strTemplatePath = Server.MapPath("~/App_UI/Reports/Templates/สัญญาการยืมเงิน.docx");
                string strSaveAsFileName = string.Format("สัญญาการยืมเงิน{0}.docx", DateTime.Now.ToString("yyyyMMddHHmmssffff"));
                string strSaveAsPath = Server.MapPath("~/App_UI/Reports/Temp/");

                if (mailMerge.MergeTemplate(strTemplatePath, strSaveAsPath + strSaveAsFileName, fields, Table, null, null))
                {
                    mailMerge.SaveFileToClient(Response, strSaveAsPath, strSaveAsFileName, true);
                }
            }
            else
            {
                return;
            }
            */
        }

        protected void lbtnContBorr_Click(object sender, EventArgs e)
        {
            string strMoneyReqSeq = string.Empty;

            if ((lblMoneyReqSeq.Text.Trim() == "") & (GrdViewAll.SelectedIndexes.Count > 0))
            {
                GridDataItem item = GrdViewAll.Items[GrdViewAll.SelectedIndexes[0]];
                strMoneyReqSeq = main.WebString(item["money_req_seq"].Text);
            }
            else if (lblMoneyReqSeq.Text.Trim() != "")
            {
                strMoneyReqSeq = lblMoneyReqSeq.Text;
            }
            else
            {
                strMoneyReqSeq = "0";
            }

            lbtnContBorr.PostBackUrl = string.Format("~/App_UI/Forms/frmGetDocxReport.aspx/?arg1={0}&arg2={1}&arg3={2}", "CB", strMoneyReqSeq, "");

        }

        protected void cmdReportMoneyReq1_Click(object sender, EventArgs e)
        {
            /*Modified by Raksakaew on Feb 26,2014
            if (!PanelSummary.Visible && rblFilterEventSeq.SelectedValue != "12")
            {
                main.MessageAlert(Page, "รองรับรายการยิมเงินเฉพาะหน้าจอ [สรุปรายการ] เท่านั้น");
                return;
            }*/

            string strMoneyReqSeq = string.Empty;

            if ((lblMoneyReqSeq.Text.Trim() == "") & (GrdViewAll.SelectedIndexes.Count > 0))
            {
                GridDataItem item = GrdViewAll.Items[GrdViewAll.SelectedIndexes[0]];
                strMoneyReqSeq = main.WebString(item["money_req_seq"].Text);
            }
            else
            {
                strMoneyReqSeq = lblMoneyReqSeq.Text;
            }

            int nMoneyReqSeq = (main.ConvertNullInt(strMoneyReqSeq) ?? 0);

            cmdReportMoneyReq1.PostBackUrl = string.Format("~/App_UI/Forms/frmGetDocxReport.aspx/?arg1={0}&arg2={1}&arg3={2}", "C", nMoneyReqSeq, "");

            /*
            DataTable dtbMain = req2.ReportMain(nMoneyReqSeq);
            DataTable dtbSub = req2.ReportSub(nMoneyReqSeq);

            Dictionary<string, string> fields = new Dictionary<string, string>();

            if (dtbMain.Rows.Count > 0)
            {
                DataRow dr = dtbMain.Rows[0];
                int nLastStepNo = (main.ConvertNullInt(send.GetLastStepNo("FIM_MONEY_REQUESTS")) ?? 0);
                DataTable dtbApprover1 = send.GetCurrentApprover(dr["emp_seq"].ToString(), "FIM_MONEY_REQUESTS", 1);
                DataTable dtbApprover2 = send.GetCurrentApprover(dr["emp_seq"].ToString(), "FIM_MONEY_REQUESTS", nLastStepNo);

                fields.Add("JobSiteName", dr["job_site_name"].ToString());
                fields.Add("MoneyReqName", dr["money_req_name"].ToString());
                fields.Add("MoneyReqDate", main.ConvertNullDateTime(dr["money_req_date"]) != null
                    ? main.ConvertNullDateTime(dr["money_req_date"]).Value.ToLongDateString() : "");
                fields.Add("BorrowStartDate", main.ConvertNullDateTime(dr["borrow_start_date"]) != null
                    ? main.ConvertNullDateTime(dr["borrow_start_date"]).Value.ToLongDateString() : "");
                fields.Add("BorrowEndDate", main.ConvertNullDateTime(dr["borrow_end_date"]) != null
                    ? main.ConvertNullDateTime(dr["borrow_end_date"]).Value.ToLongDateString() : "");
                fields.Add("EmpName", dr["emp_name"].ToString());
                fields.Add("PayeeName", dr["payee_name"].ToString());
                fields.Add("ProjectName", dr["bgm_proj_name"].ToString());

                if (dtbApprover1.Rows.Count > 0)
                {
                    DataTable dtb1 = emplist.GetInfo(main.ConvertNullInt(dtbApprover1.Rows[0]["emp_seq"]) ?? 0);
                    if (dtb1.Rows.Count > 0)
                    {
                        fields.Add("ApproverName", dtb1.Rows[0]["full_name"].ToString());
                        fields.Add("ApproverPosName", dtb1.Rows[0]["pos_name"].ToString());
                    }
                }

                if (dtbApprover2.Rows.Count > 0)
                {
                    DataTable dtb1 = emplist.GetInfo(main.ConvertNullInt(dtbApprover2.Rows[0]["emp_seq"]) ?? 0);
                    if (dtb1.Rows.Count > 0)
                    {
                        fields.Add("ApproverName2", dtb1.Rows[0]["full_name"].ToString());
                        fields.Add("ApproverPosName2", dtb1.Rows[0]["pos_name"].ToString());
                    }
                }
            }

            Dictionary<string, DataTable> dd = new Dictionary<string, DataTable>();
            dd.Add("table1", dtbSub);

            string strTemplatePath = Server.MapPath("~/App_UI/Reports/Templates/แบบฟอร์ม_เบิกเงิน.docx");
            string strSaveAsFileName = string.Format("แบบฟอร์ม_เบิกเงิน_{0}.docx", DateTime.Now.ToString("yyyyMMddHHmmss"));
            string strSaveAsPath = Server.MapPath("~/App_UI/Reports/Temp/");

            if (mailMerge.MergeTemplate(strTemplatePath, strSaveAsPath + strSaveAsFileName, fields, dd, null, null))
            {
                mailMerge.SaveFileToClient(Response, strSaveAsPath, strSaveAsFileName, true);               
            }
            */
        }

        protected void cmdInsert2_Click(object sender, EventArgs e)
        {
            // check user pressing save button as double time.
            // logic: use guid in "lblInsertUid.Text", user can save is that guid never been used
            //        if the guid used, alert warning and close tooltip popup
            if (InsertGuidSessionManager.UseGuid(lblInsertUid.Text) == false)
            {
                main.MessageAlert(Page, "ท่านได้บันทึกข้อมูลซ้ำติดกันสองครั้ง ระบบจะทำการบันทึกรายการให้เพียงรายการเดียวเพื่อไม่ให้ข้อมูลซ้ำ");
                ClearControl2();
                PanelProjOperExp.Visible = false;
                PanelOpacity.Visible = false;
                LoadGrdMoneyRequestDtls();
                //LoadRepeater4();
                SetProgress(lblMoneyReqSeq.Text);
                rblBgmExpGrpSeq.Enabled = false;
                return;
            }

            if (!CheckData2())
            {
                // regenerate guid because current guid is already use in InsertGuidSessionManager.UseGuid(lblInsertUid.Text)
                lblInsertUid.Text = InsertGuidSessionManager.GenerateNewGuid();
                return;
            }

            if (!CheckAmount(lblProjOperRemainAmount.Text, txtAmount.Text))
            {
                // regenerate guid because current guid is already use in InsertGuidSessionManager.UseGuid(lblInsertUid.Text)
                lblInsertUid.Text = InsertGuidSessionManager.GenerateNewGuid();
                main.MessageAlert(Page, "จำนวนเงินที่ขอเกินกว่าเงินที่ได้รับอนุมัติ");
                return;
            }

            Nullable<int> nBgmProjOperExpSeq = main.ConvertNullInt(lblBgmProjOperExpSeq.Text);
            Nullable<int> nMoneyReqSeq = main.ConvertNullInt(lblMoneyReqSeq.Text);
            Nullable<int> nBgmExpGrpSeq = main.ConvertNullInt(rblBgmExpGrpSeq.SelectedValue);
            Nullable<double> dAmount = panelTotalAmount.Visible ? main.ConvertNullDouble(lblTotalAmount.Text) : main.ConvertNullDouble(txtAmount.Text);
            double? dPercentVat = panelVat.Visible ? main.ConvertNullDouble(txtPercentVat.Text) : 0;
            double? dPercentTax = panelTax.Visible ? main.ConvertNullDouble(txtPercentTax.Text) : 0;
            double? dAmountVat = panelVat.Visible ? main.ConvertNullDouble(txtAmountVat.Text) : 0;
            double? dAmountTax = panelTax.Visible ? main.ConvertNullDouble(txtAmountTax.Text) : 0;
            Nullable<int> nEmpSeq = main.ConvertNullInt(Session["EmpSeq"]);

            FIM_MoneyRequests.M_FIM_MoneyRequests reqInfo = new FIM_MoneyRequests.M_FIM_MoneyRequests();
            D_FimMoneyRequest RefGet = new D_FimMoneyRequest();
            DataTable dt = new DataTable();
            //reqInfo.ref_no = req.GenerateNextMoneyRequestRefNo();
            //reqInfo.borrow_no = req.GenerateNextMoneyRequestBorrowNumber();
            reqInfo.money_req_seq = nMoneyReqSeq.Value;
            reqInfo.bgm_exp_grp_seq = nBgmExpGrpSeq;
            req.UpdateBgmExpGrpSeq(reqInfo);

            FIM_MoneyRequestDtls.M_FIM_MoneyRequestDtls info = new FIM_MoneyRequestDtls.M_FIM_MoneyRequestDtls();
            info.money_req_seq = nMoneyReqSeq;
            info.bgm_proj_oper_exp_seq = nBgmProjOperExpSeq;
            info.amount = dAmount;
            info.percent_vat = dPercentVat;
            info.percent_tax = dPercentTax;
            info.amount_vat = dAmountVat;
            info.amount_tax = dAmountTax;
            info.remarks = txtRemarks.Text;

            info.created_by = nEmpSeq;
            info.created_when = DateTime.Today;

            info.ConId = main.ConvertNullInt(ddlVender.SelectedValue);
            info.ref_no = txtDtlsRefNo.Text;
            info.ref_date = txtDtlsRefDate.GetDateValue();
            info.branch_no = txtBranchNo.Text;

            int nMoneyReqDtlsSeq = Convert.ToInt32(reqDtls.Insert(info));

            if (PanelExpRate.Visible || PanelExpRateEmp.Visible)
            {
                Repeater use = null;
                if (PanelExpRate.Visible)
                {
                    use = RepeaterExpRate;
                }
                else
                {
                    use = RepeaterExpRateEmp;
                }

                foreach (RepeaterItem item in use.Items)
                {
                    FIM_MoneyRequestDtlsRates.M_FIM_MoneyRequestDtlsRates rateInfo = new FIM_MoneyRequestDtlsRates.M_FIM_MoneyRequestDtlsRates();
                    string strTotalAmount = main.WebString(((Label)item.FindControl("lblTotalAmount")).Text);
                    string strMoneyReqDtlsRateSeq = main.WebString(((Label)item.FindControl("lblMoneyReqDtlsRateSeq")).Text);

                    if (main.ConvertNullDouble(strTotalAmount) > 0)
                    {
                        RadNumericTextBox txtMan = (RadNumericTextBox)item.FindControl("txtMan");
                        RadNumericTextBox txtDay = (RadNumericTextBox)item.FindControl("txtDay");
                        RadNumericTextBox txtHour = (RadNumericTextBox)item.FindControl("txtHour");
                        RadNumericTextBox txtMinute = (RadNumericTextBox)item.FindControl("txtMinute");

                        rateInfo.money_req_dtls_seq = nMoneyReqDtlsSeq;

                        rateInfo.amount = main.ConvertNullDouble(((RadNumericTextBox)item.FindControl("txtAmount")).Text);

                        if (PanelExpRateEmp.Visible)
                        {
                            rateInfo.emp_seq = main.ConvertNullInt(((Label)item.FindControl("lblEmpSeq")).Text);
                            rateInfo.class_seq = main.ConvertNullInt(((Label)item.FindControl("lblClassSeq")).Text);
                            rateInfo.exp_rate_seq = main.ConvertNullInt(((RadComboBox)item.FindControl("ddlExpRateSeq")).SelectedValue);
                        }
                        else
                        {
                            rateInfo.exp_rate_seq = main.ConvertNullInt(((Label)item.FindControl("lblExpRateSeq")).Text);
                        }

                        if (txtMan.Visible)
                        {
                            rateInfo.man = main.ConvertNullInt(((RadNumericTextBox)item.FindControl("txtMan")).Text);
                        }
                        else
                        {
                            rateInfo.man = null;
                        }

                        if (txtDay.Visible)
                        {
                            rateInfo.day = main.ConvertNullDouble(((RadNumericTextBox)item.FindControl("txtDay")).Text);
                        }
                        else
                        {
                            rateInfo.day = null;
                        }

                        if (txtHour.Visible)
                        {
                            rateInfo.hour = main.ConvertNullDouble(((RadNumericTextBox)item.FindControl("txtHour")).Text);
                        }
                        else
                        {
                            rateInfo.hour = null;
                        }

                        if (txtMinute.Visible)
                        {
                            rateInfo.minute = main.ConvertNullDouble(((RadNumericTextBox)item.FindControl("txtMinte")).Text);
                        }
                        else
                        {
                            rateInfo.minute = null;
                        }

                        if (strMoneyReqDtlsRateSeq.Length == 0)
                        {
                            rateInfo.created_by = nEmpSeq;
                            rateInfo.created_when = DateTime.Today;
                            reqRate.Insert(rateInfo);
                        }
                        else
                        {
                            rateInfo.money_req_dtls_rate_seq = main.ConvertNullInt(strMoneyReqDtlsRateSeq);
                            rateInfo.updated_by = nEmpSeq;
                            rateInfo.updated_when = DateTime.Today;
                            reqRate.Update(rateInfo);
                        }
                    }
                    else if (strMoneyReqDtlsRateSeq.Length > 0)
                    {
                        rateInfo.money_req_dtls_rate_seq = main.ConvertNullInt(strMoneyReqDtlsRateSeq);
                        rateInfo.updated_by = nEmpSeq;
                        rateInfo.updated_when = DateTime.Today;
                        reqRate.Delete(rateInfo);
                    }

                }
            }

            ClearControl2();
            PanelProjOperExp.Visible = false;
            PanelOpacity.Visible = false;
            LoadGrdMoneyRequestDtls();
            //LoadRepeater4();
            SetProgress(lblMoneyReqSeq.Text);
            rblBgmExpGrpSeq.Enabled = false;
        }

        protected void cmdUpdate2_Click(object sender, EventArgs e)
        {
            if (!CheckData2())
                return;

            if (!CheckAmount(lblProjOperRemainAmount.Text, txtAmount.Text))
            {
                main.MessageAlert(Page, "จำนวนเงินที่ขอเกินกว่าเงินที่ได้รับอนุมัติ");
                return;
            }

            int nMoneyReqDtlsSeq = Convert.ToInt32(lblMoneyReqDtlsSeq.Text);
            Nullable<int> nBgmProjOperExpSeq = main.ConvertNullInt(lblBgmProjOperExpSeq.Text);
            Nullable<int> nMoneyReqSeq = main.ConvertNullInt(lblMoneyReqSeq.Text);
            Nullable<double> dAmount = panelTotalAmount.Visible ? main.ConvertNullDouble(lblTotalAmount.Text) : main.ConvertNullDouble(txtAmount.Text);
            double? dPercentVat = panelVat.Visible ? main.ConvertNullDouble(txtPercentVat.Text) : 0;
            double? dPercentTax = panelTax.Visible ? main.ConvertNullDouble(txtPercentTax.Text) : 0;
            double? dAmountVat = panelVat.Visible ? main.ConvertNullDouble(txtAmountVat.Text) : 0;
            double? dAmountTax = panelTax.Visible ? main.ConvertNullDouble(txtAmountTax.Text) : 0;
            Nullable<int> nEmpSeq = main.ConvertNullInt(Session["EmpSeq"]);

            FIM_MoneyRequestDtls.M_FIM_MoneyRequestDtls info = new FIM_MoneyRequestDtls.M_FIM_MoneyRequestDtls();
            info.money_req_dtls_seq = nMoneyReqDtlsSeq;
            info.money_req_seq = nMoneyReqSeq;
            info.bgm_proj_oper_exp_seq = nBgmProjOperExpSeq;
            info.amount = dAmount;
            info.percent_vat = dPercentVat;
            info.percent_tax = dPercentTax;
            info.amount_vat = dAmountVat;
            info.amount_tax = dAmountTax;
            info.remarks = txtRemarks.Text;

            info.updated_by = nEmpSeq;
            info.updated_when = DateTime.Today;
            info.ConId = main.ConvertNullInt(ddlVender.SelectedValue);
            info.ref_no = txtDtlsRefNo.Text;
            info.ref_date = txtDtlsRefDate.GetDateValue();
            info.branch_no = txtBranchNo.Text;

            reqDtls.Update(info);

            if (PanelExpRate.Visible || PanelExpRateEmp.Visible)
            {
                Repeater use = null;
                if (PanelExpRate.Visible)
                {
                    use = RepeaterExpRate;
                }
                else
                {
                    use = RepeaterExpRateEmp;
                }

                foreach (RepeaterItem item in use.Items)
                {
                    FIM_MoneyRequestDtlsRates.M_FIM_MoneyRequestDtlsRates rateInfo = new FIM_MoneyRequestDtlsRates.M_FIM_MoneyRequestDtlsRates();
                    string strTotalAmount = main.WebString(((Label)item.FindControl("lblTotalAmount")).Text);
                    string strMoneyReqDtlsRateSeq = main.WebString(((Label)item.FindControl("lblMoneyReqDtlsRateSeq")).Text);

                    if (main.ConvertNullDouble(strTotalAmount) > 0)
                    {
                        RadNumericTextBox txtMan = (RadNumericTextBox)item.FindControl("txtMan");
                        RadNumericTextBox txtDay = (RadNumericTextBox)item.FindControl("txtDay");
                        RadNumericTextBox txtHour = (RadNumericTextBox)item.FindControl("txtHour");
                        RadNumericTextBox txtMinute = (RadNumericTextBox)item.FindControl("txtMinute");

                        rateInfo.money_req_dtls_seq = nMoneyReqDtlsSeq;
                        rateInfo.amount = main.ConvertNullDouble(((RadNumericTextBox)item.FindControl("txtAmount")).Text);

                        if (PanelExpRate.Visible)
                        {
                            rateInfo.exp_rate_seq = main.ConvertNullInt(((Label)item.FindControl("lblExpRateSeq")).Text);
                        }
                        else
                        {
                            rateInfo.exp_rate_seq = main.ConvertNullInt(((RadComboBox)item.FindControl("ddlExpRateSeq")).SelectedValue);
                            rateInfo.emp_seq = main.ConvertNullInt(((Label)item.FindControl("lblEmpSeq")).Text);
                            rateInfo.class_seq = main.ConvertNullInt(((Label)item.FindControl("lblClassSeq")).Text);
                        }

                        if (txtMan.Visible)
                        {
                            rateInfo.man = main.ConvertNullInt(((RadNumericTextBox)item.FindControl("txtMan")).Text);
                        }
                        else
                        {
                            rateInfo.man = null;
                        }

                        if (txtDay.Visible)
                        {
                            rateInfo.day = main.ConvertNullDouble(((RadNumericTextBox)item.FindControl("txtDay")).Text);
                        }
                        else
                        {
                            rateInfo.day = null;
                        }

                        if (txtHour.Visible)
                        {
                            rateInfo.hour = main.ConvertNullDouble(((RadNumericTextBox)item.FindControl("txtHour")).Text);
                        }
                        else
                        {
                            rateInfo.hour = null;
                        }

                        if (txtMinute.Visible)
                        {
                            rateInfo.minute = main.ConvertNullDouble(((RadNumericTextBox)item.FindControl("txtMinte")).Text);
                        }
                        else
                        {
                            rateInfo.minute = null;
                        }

                        if (strMoneyReqDtlsRateSeq.Length == 0)
                        {
                            rateInfo.created_by = nEmpSeq;
                            rateInfo.created_when = DateTime.Today;
                            reqRate.Insert(rateInfo);
                        }
                        else
                        {
                            rateInfo.money_req_dtls_rate_seq = main.ConvertNullInt(strMoneyReqDtlsRateSeq);
                            rateInfo.updated_by = nEmpSeq;
                            rateInfo.updated_when = DateTime.Today;
                            reqRate.Update(rateInfo);
                        }
                    }
                    else if (strMoneyReqDtlsRateSeq.Length > 0)
                    {
                        rateInfo.money_req_dtls_rate_seq = main.ConvertNullInt(strMoneyReqDtlsRateSeq);
                        rateInfo.updated_by = nEmpSeq;
                        rateInfo.updated_when = DateTime.Today;
                        reqRate.Delete(rateInfo);
                    }
                }
            }

            ClearControl2();
            PanelProjOperExp.Visible = false;
            PanelOpacity.Visible = false;
            LoadGrdMoneyRequestDtls();
            //LoadRepeater4();
            InsertGuidSessionManager.UseGuid(lblInsertUid.Text);
        }

        protected void cmdCancel2_Click(object sender, EventArgs e)
        {
            ClearControl2();
            PanelProjOperExp.Visible = false;
            PanelOpacity.Visible = false;
        }

        protected void cmdAmountMax_Click(object sender, EventArgs e)
        {
            RepeaterItem item = (RepeaterItem)((LinkButton)sender).Parent;

            ((RadNumericTextBox)item.FindControl("txtAmount")).Text = ((HtmlInputHidden)item.FindControl("hAmountMax")).Value;
            CalculateTotalAmount(sender);
        }

        protected void cmdExportToMsWord_Click(object sender, EventArgs e)
        {
            ExportToMsWord();
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            string strMoneyReqSeq = lblMoneyReqSeq.Text;
            KPIS.GERP.ESS.APP.FIM_MoneyRequests.M_FIM_MoneyRequests currentMoneyRequest;

            if (lblApproveEmpSeq.Text.Length == 0 && PanelSendTo.Visible)
            {
                ((RadAjaxManager)Master.FindControl("RadAjaxManager1")).Alert("คำเตือน!! กรุณาเลือกผู้อนุมัติก่อนส่ง");
                return;
            }
            if ((currentMoneyRequest = req.GetInfo(strMoneyReqSeq)) == null)
            {
                ((RadAjaxManager)Master.FindControl("RadAjaxManager1")).Alert("ไม่พบข้อมูล ขอเบิก/ยืมเงินนี้");
                return;
            }

            try
            {
                ESS.MODEL.M_MessageSend info = new ESS.MODEL.M_MessageSend();
                info.send_date = DateTime.Today;
                info.ref_seq = main.ConvertNullInt(strMoneyReqSeq);
                info.ref_type = "fim_money_requests";
                info.ref_no = currentMoneyRequest.ref_no;
                info.send_text = "ส่งคำขอเบิก/ยืมเงิน : " + lblFinMoneyReqName.Text;
                info.from_emp_seq = main.ConvertNullInt(Session["EmpSeq"]);
                info.to_emp_seq = main.ConvertNullInt(lblApproveEmpSeq.Text);

                info.from_pos_seq = emp.GetInfo(Session["EmpSeq"].ToString()).pos_seq;
                info.to_pos_seq = main.ConvertNullInt(lblApprovePosSeq.Text);
                info.wod_emp_seq = null;

                info.from_send_seq = null;
                info.remarks = null;
                info.message_status = "0";
                info.approve_path_seq = main.ConvertNullInt(lblApprovePathSeq.Text);
                info.created_by = main.ConvertNullInt(Session["EmpSeq"]);
                info.created_when = DateTime.Now;

                if (PanelSendTo2.Visible)
                {
                    info.pos_grp_seq = main.ConvertNullInt(lblPosGrpSeq.Text);
                    info.to_pos_seq = null;
                    info.to_emp_seq = null;
                }

                send.Insert(info);

                FIM_MoneyRequests.M_FIM_MoneyRequests info2 = new FIM_MoneyRequests.M_FIM_MoneyRequests();
                info2.money_req_seq = Convert.ToInt32(strMoneyReqSeq);
                info2.record_status = "P";
                info2.updated_by = main.ConvertNullInt(Session["EmpSeq"]);
                info2.updated_when = DateTime.Now;
                req.ChangeStatus(info2);
                cmdSummary_Click(null, null);
                //lblRecordStatus.Text = "P";
                //lblFinRecordStatus.Text = "P";
                //lblFinRecordStatusName.Text = "รอการอนุมัติ";
                //cmdSendMessage1.Visible = false;
                //lblFinRecordStatusName.ForeColor = System.Drawing.Color.Blue;
                ((RadAjaxManager)Master.FindControl("RadAjaxManager1")).Alert("บันทึกเรียบร้อยแล้ว");
                //RadAjaxManager1.Alert("บันทึกเรียบร้อยแล้ว");

                PanelMessageSend.Visible = false;
                //ScriptManager.RegisterClientScriptBlock(
                //    this.Page,
                //    this.GetType(),
                //    "WebUserControlSript",
                //    "CloseActiveToolTip()",
                //    true);
            }
            catch
            {
                ((RadAjaxManager)Master.FindControl("RadAjaxManager1")).Alert("เกิดข้อผิดพลาดในการบันทึกข้อมูล");
                //RadAjaxManager1.Alert("เกิดข้อผิดพลาดในการบันทึกข้อมูล");
            }
        }

        protected void cmdSendMessage1_Click(object sender, EventArgs e)
        {
            PanelMessageSend.Visible = true;
            ddlApprover.SelectedIndex = -1;
            lblAppClassName.Text = "";
            lblAppExecPosName.Text = "";
            lblAppJobsiteName.Text = "";
            lblAppPosName.Text = "";
            lblApproveEmpSeq.Text = "";
            lblApprovePosSeq.Text = "";

            //if (lblFinRecordStatus.Text == "R")
            //{
            //    var model = send.GetLatestMessageStatus(lblMoneyReqSeq.Text, "fim_money_requests");
            //    lblApprovePathSeq.Text = model.approve_path_seq.ToString();
            //    ddlApprover.SelectedValue = model.to_emp_seq.ToString();
            //    ddlApprover_SelectedIndexChanged(null, null);
            //}
            //else
            //{
            LoadApprover();
            //}
        }

        protected void chkIncludeVat_CheckedChanged(object sender, EventArgs e)
        {
            panelVat.Visible = chkIncludeVat.Checked;
            panelTotalAmount.Visible = chkIncludeVat.Checked || chkIncludeTax.Checked;
            txtPercentVat.Text = panelVat.Visible ? "7" : "0";
            CalculateAmount(2);
        }

        protected void txtPercentVat_TextChanged(object sender, EventArgs e)
        {
            CalculateAmount(2);
        }

        protected void chkIncludeTax_CheckedChanged(object sender, EventArgs e)
        {
            panelTax.Visible = chkIncludeTax.Checked;
            panelTotalAmount.Visible = chkIncludeVat.Checked || chkIncludeTax.Checked;
            txtPercentTax.Text = panelTax.Visible ? "1" : "0";
            CalculateAmount(5);
        }

        protected void txtPercentTax_TextChanged(object sender, EventArgs e)
        {
            CalculateAmount(5);
        }

        //protected void CalculateAmount()
        //{
        //    if (panelTotalAmount.Visible)
        //    { 
        //        double dAmount = main.ConvertNullDouble(txtAmount.Text) ?? 0;
        //        double dPercentVat = main.ConvertNullDouble(txtPercentVat.Text) ?? 0;
        //        double dPercentTax = main.ConvertNullDouble(txtPercentTax.Text) ?? 0;
        //        double dAmountVat = main.ConvertNullDouble(txtAmountVat.Text) ?? 0;
        //        double dAmountTax = main.ConvertNullDouble(txtAmountTax.Text) ?? 0;
        //        double dExcludeVatAmount = main.ConvertNullDouble(lblExcludeVat.Text) ?? 0;
        //        double dTotalAmount = main.ConvertNullDouble(lblTotalAmount.Text) ?? 0;

        //        if (dPercentVat > 0)
        //        {
        //            dAmountVat = dAmount * 7 / (100 + dPercentVat);
        //        }
        //        else
        //        {
        //            dAmountVat = 0;
        //        }
        //        dExcludeVatAmount = dAmount - dAmountVat;

        //        dAmountTax = dExcludeVatAmount * dPercentTax / 100;
        //        dTotalAmount = dAmount;

        //        txtAmountVat.Text = dAmountVat.ToString("#,##0.00");
        //        txtAmountTax.Text = dAmountTax.ToString("#,##0.00");
        //        lblExcludeVat.Text = dExcludeVatAmount.ToString("#,##0.00");
        //        lblTotalAmount.Text = dTotalAmount.ToString("#,##0.00");
        //    }
        //}

        protected void CalculateAmount(int nStep)
        {
            double dAmount = main.ConvertNullDouble(txtAmount.Text) ?? 0;
            double dPercentVat = main.ConvertNullDouble(txtPercentVat.Text) ?? 0;
            double dPercentTax = main.ConvertNullDouble(txtPercentTax.Text) ?? 0;
            double dAmountVat = main.ConvertNullDouble(txtAmountVat.Text) ?? 0;
            double dAmountTax = main.ConvertNullDouble(txtAmountTax.Text) ?? 0;
            double dExcludeVatAmount = main.ConvertNullDouble(lblExcludeVat.Text) ?? 0;
            double dNetAmount = main.ConvertNullDouble(lblTotalAmount.Text) ?? 0;

            switch (nStep)
            {
                case 1:
                case 2:
                    if (dPercentVat > 0)
                    {
                        dAmountVat = dAmount * 7 / (100 + dPercentVat);//dAmount * dPercentVat / 100;
                    }
                    else
                    {
                        dAmountVat = 0;
                    }
                    dExcludeVatAmount = dAmount - dAmountVat;

                    dAmountTax = dExcludeVatAmount * dPercentTax / 100;
                    dNetAmount = dAmount;
                    break;
                case 3:
                    dExcludeVatAmount = dAmount - dAmountVat;

                    dAmountTax = dExcludeVatAmount * dPercentTax / 100;
                    dNetAmount = dAmount;
                    break;
                case 4:
                    dAmountVat = dAmount - dExcludeVatAmount;

                    dAmountTax = dExcludeVatAmount * dPercentTax / 100;
                    dNetAmount = dAmount;
                    break;
                case 5:
                case 6:
                    dAmountTax = dExcludeVatAmount * dPercentTax / 100;
                    dNetAmount = dAmount;
                    break;
            }

            txtAmountVat.Text = dAmountVat.ToString("#,##0.00");
            txtAmountTax.Text = dAmountTax.ToString("#,##0.00");
            lblExcludeVat.Text = dExcludeVatAmount.ToString("#,##0.00");
            lblTotalAmount.Text = dNetAmount.ToString("#,##0.00");
        }

        protected void txtAmountVat_TextChanged(object sender, EventArgs e)
        {
            CalculateAmount(3);
        }

        protected void txtAmount_TextChanged1(object sender, EventArgs e)
        {
            CalculateAmount(1);
        }

        protected void btAddBgmExpGrpSeqNew_Click(object sender, EventArgs e)
        {
            //ddlBgmExpGrpSeqNew.Enabled = false;
            ////btAddBgmExpGrpSeqNew.Enabled = false;
            //grdMoneyRequestDtlsNew.Visible = false;
            //GrdAddConAndMoney.Visible = false;
            ////PanelPayeeType.Enabled = false;
            //RadGrid grid = new RadGrid();
            //grid.AutoGenerateColumns = false;
            //if (rblPayeeTypeNew.SelectedValue == "2")
            //{
            //    //GridTemplateColumn tmpColumn = new GridTemplateColumn();
            //    NewTemplateColumn templateColumn = new NewTemplateColumn();
            //    templateColumn.ItemTemplate = new TemplateColumn();
            //    templateColumn.HeaderText = "รายชื่อ";
            //    grid.MasterTableView.Columns.Add(templateColumn);
            //}
            //else
            //{
            //    GridTemplateColumn tmpColumn = new GridTemplateColumn();


            //    grid.MasterTableView.Columns.Add(tmpColumn);
            //}

            //foreach (GridItem item in grdMoneyRequestDtlsNew.MasterTableView.Items)
            //{
            //    GridDataItem dataitem = (GridDataItem)item;
            //    TableCell cell = dataitem["GridCheckBoxColumn"];
            //    CheckBox checkBox = (CheckBox)cell.Controls[0];
            //    if (checkBox.Checked)
            //    {
            //        GridTemplateColumn tmpColumn = new GridTemplateColumn();
            //        tmpColumn.HeaderText = "ค่าใช่จ่าย";
            //        //NewTemplateColumn templateColumn = new NewTemplateColumn();
            //        //templateColumn.ItemTemplate = new TemplateColumn();

            //        grid.MasterTableView.Columns.Add(tmpColumn); //Add column before setting properties
            //    }
            //}
            //grid.AllowPaging = true;
            //grid.PageSize = 3;
            //grid.Skin = "Outlook";
            //grid.Visible = true;
            //PanelGrid.Controls.Add(grid);
        }

        private static void ShowCheckedItems(RadComboBox comboBox, Literal literal)
        {
            var sb = new StringBuilder();
            var collection = comboBox.CheckedItems;

            if (collection.Count != 0)
            {
                sb.Append("<ul>");

                foreach (var item in collection)
                    sb.Append("<li>" + item.Text + "</li>");

                sb.Append("</ul>");

                literal.Text = sb.ToString();
            }
            else
            {
                literal.Text = "<p>ไม่มีรายชื่อที่เลือก</p>";
            }
        }

        protected void cmdCancelMessage_Click(object sender, EventArgs e)
        {
            var message = send.GetInfoFromRefSeq(lblMoneyReqSeq.Text, "fim_money_requests");
            message.updated_by = SsMng.EmpSeqI;
            message.updated_when = DateTime.Now;
            send.Delete(message);

            var moneyReq = req.GetInfo(lblMoneyReqSeq.Text);
            moneyReq.record_status = "W";
            moneyReq.updated_by = SsMng.EmpSeqI;
            moneyReq.updated_when = DateTime.Now;
            req.ChangeStatus(moneyReq);

            cmdProgress1_Click(null, null);
        }
        #endregion

        #region Private Methods

        /*Modified by Raksakaew on Feb 26,2014 move to loadData2UI
        private void loadData2UI(string strMoneyReqSeq, FIM_MoneyRequests.M_FIM_MoneyRequests info)
        {

            lblMoneyReqSeq.Text = strMoneyReqSeq;
            lblMoneyReqDate.Text = info.money_req_date.Value.ToShortDateString();
            txtBgmYear.Text = info.bgm_year.ToString();
            ddlBgmSourceSeq.SelectedValue = info.bgm_source_seq.ToString();
            ddlBgmSourceSeq_SelectedIndexChanged(null, null);
            lblRefNo.Text = info.ref_no.ToString();
            ddlBgmProjOperSeq.SelectedValue = info.bgm_proj_oper_seq.ToString();
            txtMoneyReqName.Text = info.money_req_name;
            lblRecordStatus.Text = info.record_status;
            lblFinRecordStatus.Text = info.record_status;
            lblFinRecordStatusName.Text = info.record_status_name;
            rblEventSeq.SelectedValue = info.event_seq.ToString();
            rblEventSeq_SelectedIndexChanged(null, null);
            rblBgmExpGrpSeq.SelectedValue = (info.bgm_exp_grp_seq != null ? info.bgm_exp_grp_seq.ToString() : "2");

            lblMoneyReqName.Text = info.money_req_name;
            lblMoneyReqName2.Text = info.money_req_name;

            if (rblEventSeq.SelectedValue == "16" || rblEventSeq.SelectedValue == "18")
            {
                rblBorrowTypeSeq.SelectedValue = info.borrow_type_seq.ToString();
                rblBorrowTypeSeq_SelectedIndexChanged(null, null);

                dpBorrowStartDate.SetValue(info.borrow_start_date);
                dpBorrowEndDate.SetValue(info.borrow_end_date);
                lblBorrowReturnDate.Text = main.ConvertDBDateTimeToString(info.borrow_return_date, false);

                if (li3.Visible)
                {
                    LoadRepeaterPerson();
                }
            }
            else if (rblEventSeq.SelectedValue == "19")
            {
                rblTransferTypeSeq.SelectedValue = info.transfer_type_seq.ToString();
                rblTransferTypeSeq_SelectedIndexChanged(null, null);

                ddlJobSiteSeq.SelectedValue = info.transfer_job_site_seq.ToString();
                dpBorrowStartDate2.SetValue(info.borrow_start_date);
                dpBorrowEndDate2.SetValue(info.borrow_end_date);
                lblBorrowReturnDate2.Text = main.ConvertDBDateTimeToString(info.borrow_return_date, false);

                if (li3.Visible)
                {
                    LoadRepeaterPerson();
                }
            }
        }*/

        #endregion

        #region Protected Methods

        protected void RegisClientScript()
        {
            RadAjaxManager manager = RadAjaxManager.GetCurrent(Page);
            manager.AjaxRequest += new RadAjaxControl.AjaxRequestDelegate(ReturnDateAjaxRequest);
        }

        /*==================== main code area ========================*/
        protected void LoadGrdViewAll()
        {
            var data = req.GetFull(null, txtFilterBgmYear.Text, null
                , rblFilterEventSeq.SelectedValue, null, Session["EmpSeq"].ToString(), false);

            var data3 = new List<KPIS.GERP.ESS.APP.FIM_MoneyRequests.M_FIM_MoneyRequests>(data);
            data3.Reverse(0, data3.Count);
            GrdViewAll.DataSource = data3;
            GrdViewAll.DataBind();
        }

        protected void SetPagePermission()
        {
            //ViewState["Permission"] = null;//"user-1";//main.GetPermission(Session["UserSeq"]);
            bool bFinance = SsMng.WorkingJobSiteSeqI == new FIM_SystemControls.FIM_SystemControls().GetInfo().finance_job_site_seq;

            txtMoneyReqDate.Visible = bFinance;
            lblMoneyReqDate.Visible = !bFinance;
        }

        protected void SetControl(string strCommand)
        {
            switch (strCommand)
            {
                case "ViewAll":
                    //cmdMuti.Visible = false;
                    cmdAddNew1.Visible = true;
                    cmdEdit1.Visible = true;
                    cmdDelete1.Visible = true;
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = false;
                    cmdCancel.Visible = false;
                    cmdSendMessage1.Visible = false;
                    //cmdInsertMuti.Visible = false;
                    lblModeName.Text = "แสดงรายการ";
                    lblMode.Text = "V";
                    cmdCancelMessage.Visible = false;
                    //cmdViewAll_Click(null, null);
                    break;
                case "AddNew":
                    //cmdMuti.Visible = true;
                    cmdAddNew1.Visible = false;
                    cmdEdit1.Visible = false;
                    cmdDelete1.Visible = false;
                    cmdInsert1.Visible = true;
                    cmdUpdate1.Visible = false;
                    cmdCancel.Visible = false;
                    cmdSendMessage1.Visible = false;
                    //cmdInsertMuti.Visible = false;
                    lblRefNo.Text = "";
                    lblModeName.Text = "เพิ่มรายการใหม่";
                    lblMode.Text = "A";
                    cmdCancelMessage.Visible = false;
                    break;
                case "Edit":
                    //cmdMuti.Visible = false;
                    cmdAddNew1.Visible = false;
                    cmdEdit1.Visible = false;
                    cmdDelete1.Visible = false;
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = true;
                    cmdCancel.Visible = true;
                    cmdSendMessage1.Visible = false;
                    //cmdInsertMuti.Visible = false;
                    lblModeName.Text = "แก้ไขรายการ";
                    lblMode.Text = "E";
                    //cmdSummary_Click(null, null);
                    break;
                case "NoControl":
                    //cmdMuti.Visible = false;
                    cmdAddNew1.Visible = false;
                    cmdEdit1.Visible = false;
                    cmdDelete1.Visible = false;
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = false;
                    cmdCancel.Visible = false;
                    cmdSendMessage1.Visible = false;
                    //cmdInsertMuti.Visible = false;

                    lblModeName.Text = "แสดงรายการ";
                    lblMode.Text = "N";
                    break;
                case "Muti":
                    //cmdMuti.Visible = true;
                    cmdAddNew1.Visible = false;
                    cmdEdit1.Visible = false;
                    cmdDelete1.Visible = false;
                    cmdInsert1.Visible = false;
                    cmdUpdate1.Visible = false;
                    cmdCancel.Visible = false;
                    cmdSendMessage1.Visible = false;
                    //cmdInsertMuti.Visible = true;
                    cmdReportMoneyReqContracts.Visible = false;
                    lbtnContBorr.Visible = false;
                    cmdReportMoneyReq1.Visible = false;
                    lblRefNo.Text = "";
                    lblModeName.Text = "เพิ่มรายการใหม่";
                    lblMode.Text = "A";
                    //cmdSummary_Click(null, null);
                    break;
            }
        }

        protected void SetBody(string strProGressID)
        {
            bool bSelected = false;
            List<Panel> PanelProgress = new List<Panel>();
            PanelProgress.Add(PanelProgress1);
            PanelProgress.Add(PanelProgress2);
            PanelProgress.Add(PanelProgress3);
            PanelProgress.Add(PanelSummary);
            PanelProgress.Add(PanelViewAll);
            //PanelProgress.Add(PanelMuti);

            List<HtmlGenericControl> liProgress = new List<HtmlGenericControl>();
            liProgress.Add(li1);
            liProgress.Add(li2);
            liProgress.Add(li3);
            liProgress.Add(liSummary);
            liProgress.Add(liViewAll);
            //liProgress.Add(liMuti);

            for (int i = 0; i < PanelProgress.Count; i++)
            {
                bSelected = (PanelProgress[i].ID == strProGressID);
                PanelProgress[i].Visible = bSelected;
                liProgress[i].Attributes["class"] = (bSelected ? "Selected" : "");
            }
        }

        protected void SetProgress(string strPK)
        {
            if (string.IsNullOrEmpty(strPK))
            {
                imgComplete1.Visible = false;
                imgInComplete1.Visible = true;
                cmdProgress2.Enabled = true;

                imgComplete2.Visible = false;
                imgInComplete2.Visible = true;
                cmdProgress2.Enabled = false;

                imgComplete3.Visible = false;
                imgInComplete3.Visible = true;
                cmdProgress3.Enabled = false;

                cmdCancelMessage.Visible = false;
                //Edit by Raksakaew on Feb 25,2014
                //cmdSummary.Enabled = false;
            }
            else
            {
                int nCount = 0;
                imgComplete1.Visible = true;
                imgInComplete1.Visible = false;

                nCount = ((IList<FIM_MoneyRequestDtls.M_FIM_MoneyRequestDtls>)reqDtls.GetList(strPK)).Count;

                imgComplete2.Visible = nCount > 0;
                imgInComplete2.Visible = nCount == 0;
                cmdProgress2.Enabled = true;

                nCount = ((IList<FIM_MoneyRequestTravelers.M_FIM_MoneyRequestTravelers>)reqTrav.GetList(strPK)).Count;

                imgComplete3.Visible = nCount > 0;
                imgInComplete3.Visible = nCount == 0;
                cmdProgress3.Enabled = true;
                cmdSummary.Enabled = true;

                string strRecordStatus = req.GetInfo(strPK).record_status;
                bool bCanEdit = (strRecordStatus == "W" || strRecordStatus == "R");// || strMessageStatus == "0");
                cmdProgress1.Enabled = bCanEdit;
                cmdProgress2.Enabled = bCanEdit;
                cmdProgress3.Enabled = bCanEdit;

                string strMessageStatus = send.GetInfoFromRefSeq(strPK, "fim_money_requests").message_status;
                cmdCancelMessage.Visible = strMessageStatus == "0";
            }
        }

        protected void ClearControl1()
        {
            lblMoneyReqSeq.Text = "";
            lblMoneyReqDate.Text = DateTime.Today.ToShortDateString();
            txtMoneyReqDate.SetValue(DateTime.Today);
            //lblMoneyReqDate1.Text = DateTime.Today.ToShortDateString();
            ddlBgmProjOperSeq.SelectedIndex = 0;
            txtMoneyReqName.Text = "";
            dpBorrowStartDate.SetValue("");
            dpBorrowEndDate.SetValue("");
            lblRecordStatus.Text = "W";
            lblBorrowReturnDate.Text = "";

            ddlBgmProjOperSeq.Enabled = true;
        }

        protected void ClearControl2()
        {
            txtAmount.Text = "";
            txtRemarks.Text = "";
            ViewState["Amount"] = 0;
        }

        protected void ClearControl3()
        {
            ddlEmpSeq.SelectedIndex = -1;
            ddlEmpSeq.Text = "";
            lblPosName.Text = "";
            lblJobsiteName.Text = "";
            lblClassName.Text = "";
        }

        //==================== optional code area =======================
        protected void LoadDDLBgmProjOperSeq()
        {
            ddlBgmProjOperSeq.DataSource = po.GetProjectListByEmpSeq(txtBgmYear.Text, ddlBgmSourceSeq.SelectedValue, Session["EmpSeq"].ToString());
            ddlBgmProjOperSeq.DataBind();

            main.InsertEmptyDDLSelect(ddlBgmProjOperSeq);
            ddlBgmProjOperSeq.SelectedIndex = 0;
        }

        //protected void LoadDDLBgmProjOperSeq1()
        //{
        //    ddlBgmProjOperSeq1.DataSource = po.GetProjectListByEmpSeq(txtBgmYear1.Text, ddlBgmSourceSeq1.SelectedValue, Session["EmpSeq"].ToString());
        //    ddlBgmProjOperSeq1.DataBind();

        //    main.InsertEmptyDDLSelect(ddlBgmProjOperSeq1);
        //    ddlBgmProjOperSeq1.SelectedIndex = 0;
        //}

        protected void LoadDDLBgmSourceSeq()
        {
            var data = source.GetList();
            ddlBgmSourceSeq.DataSource = data;
            ddlBgmSourceSeq.DataBind();

            ddlBgmProjOperSeq.SelectedIndex = 0;

            //ddlBgmSourceSeq1.DataSource = data;
            //ddlBgmSourceSeq1.DataBind();

            //ddlBgmProjOperSeq1.SelectedIndex = 0;
        }

        protected void LoadRblBgmExpGrpSeq()
        {
            rblBgmExpGrpSeq.DataSource = expGrp.GetList(null);
            rblBgmExpGrpSeq.DataBind();

            rblBgmExpGrpSeq.SelectedIndex = 3;
        }

        //protected void LoadddlBgmExpGrpSeq()
        //{
        //    ddlBgmExpGrpSeqNew.DataSource = expGrp.GetList(null);
        //    ddlBgmExpGrpSeqNew.DataBind();

        //    ddlBgmExpGrpSeqNew.SelectedIndex = 3;
        //}

        protected void LoadRblBorrowTypeSeq()
        {
            rblBorrowTypeSeq.DataSource = borrowType.GetList(ddlBgmSourceSeq.SelectedValue);
            rblBorrowTypeSeq.DataBind();

            if (rblBorrowTypeSeq.Items.Count > 0)
            {
                rblBorrowTypeSeq.SelectedIndex = 0;
                rblBorrowTypeSeq_SelectedIndexChanged(null, null);
            }
        }

        /*
        protected void LoadDDLCurrencySeq()
        {
            ddlCurrencySeq.DataSource = currency.GetList();
            ddlCurrencySeq.DataBind();

            ddlCurrencySeq.SelectedIndex = 0;
        }
        */

        protected void LoadDDLContactSeq()
        {
            ddlContactSeq.DataSource = contact.GetList(null);
            ddlContactSeq.DataBind();

            main.InsertEmptyDDLSelect(ddlContactSeq);
            ddlContactSeq.SelectedIndex = 0;
        }

        //protected void LoadDDLContactSeq2()
        //{
        //    ddlContactSeq2.DataSource = contact.GetList(null);
        //    ddlContactSeq2.DataBind();

        //    main.InsertEmptyDDLSelect(ddlContactSeq2);
        //    ddlContactSeq2.SelectedIndex = 0;
        //}

        //protected void LoadDDLContactSeq3()
        //{
        //    ddlContactSeq2.DataSource = contact.GetList(null);
        //    ddlContactSeq2.DataBind();

        //    main.InsertEmptyDDLSelect(ddlContactSeq2);
        //    ddlContactSeq2.SelectedIndex = 0;
        //}

        protected void LoadInfo()
        {
            lblWorkingJobSiteSeq.Text = SsMng.WorkingJobSiteSeq;
            lblWorkingJobSiteName.Text = SsMng.WorkingJobSiteName;
            lblWorkAreaSeq.Text = SsMng.WorkAreaSeq;

            //lblWorkingJobSiteSeq1.Text = SsMng.WorkingJobSiteSeq;
            //lblWorkingJobSiteName1.Text = SsMng.WorkingJobSiteName;
            //lblWorkAreaSeq1.Text = SsMng.WorkAreaSeq;

        }

        protected void LoadDefault()
        {
            int year = main.GetSystemControls().default_bgm_year.Value;
            txtBgmYear.Text = year.ToString();
            txtBgmYear.MinValue = year;
            txtBgmYear.MaxValue = year + 1;

            //txtBgmYear1.Text = year.ToString();
            //txtBgmYear1.MinValue = year - 20;
            //txtBgmYear1.MaxValue = year + 1;

            txtFilterBgmYear.Text = year.ToString();
            txtFilterBgmYear.MinValue = year - 20;
            txtFilterBgmYear.MaxValue = year + 1;

            lblMoneyReqDate.Text = DateTime.Today.ToShortDateString();
            txtMoneyReqDate.SetValue(DateTime.Today);
            //lblMoneyReqDate1.Text = DateTime.Today.ToShortDateString();
        }

        protected void LoadGrdMoneyRequestDtls()
        {
            string strMoneyReqSeq = lblMoneyReqSeq.Text;

            if (strMoneyReqSeq.Length == 0)
            {
                strMoneyReqSeq = null;
            }

            IList<FIM_MoneyRequestDtls.M_FIM_MoneyRequestDtls> models = reqDtls.GetExpFromProject(ddlBgmProjOperSeq.SelectedValue
                , ddlBgmSourceSeq.SelectedValue, rblBgmExpGrpSeq.SelectedValue, strMoneyReqSeq);
            double InApproveProcessAmount = reqDtls.GetExpInApproveProcess(ddlBgmProjOperSeq.SelectedValue
                , ddlBgmSourceSeq.SelectedValue, rblBgmExpGrpSeq.SelectedValue, strMoneyReqSeq);

            grdMoneyRequestDtls.DataSource = models; //ViewState["ProjectExpInfo"];

            GridGroupByExpression group1 = new GridGroupByExpression(grdMoneyRequestDtls.Columns.FindByUniqueName("m_bgm_projectoperationexpense.m_bgm_projectexpense.m_bgm_mas_expense.m_bgm_mas_expense_upper.bgm_exp_name"));
            GridGroupByExpression group2 = new GridGroupByExpression(grdMoneyRequestDtls.Columns.FindByUniqueName("m_bgm_projectoperationexpense.m_bgm_projectexpense.m_bgm_mas_expense.m_bgm_mas_expense_upper.m_bgm_mas_expense_upper.bgm_exp_name"));

            grdMoneyRequestDtls.MasterTableView.GroupByExpressions.Add(group2);
            grdMoneyRequestDtls.MasterTableView.GroupByExpressions.Add(group1);
            grdMoneyRequestDtls.DataBind();

            double dProjOperRemainAmount = poexp.GetRemainProject1(ddlBgmProjOperSeq.SelectedValue, ddlBgmSourceSeq.SelectedValue, rblBgmExpGrpSeq.SelectedValue) - (models.Sum(m => m.amount) ?? 0);
            //foreach (FIM_MoneyRequestDtls.M_FIM_MoneyRequestDtls model in models)
            //{
            //    dProjOperRemainAmount += (model.m_bgm_projectoperationexpense.remain_amount ?? 0) - (model.amount ?? 0);
            //}

            lblProjOperRemainAmount.Text = dProjOperRemainAmount.ToString("#,##0.00");
            lblProjOperRemainAmount2.Text = (dProjOperRemainAmount - InApproveProcessAmount).ToString("#,##0.00");
        }

        //protected void LoadGrdMoneyRequestDtlsNew()
        //{
        //    IList<FIM_MoneyRequestDtls.M_FIM_MoneyRequestDtls> models = null; 
        //    if (!string.IsNullOrEmpty(ddlBgmProjOperSeq1.SelectedValue) && !string.IsNullOrEmpty(ddlBgmSourceSeq1.SelectedValue) && !string.IsNullOrEmpty(ddlBgmExpGrpSeqNew.SelectedValue))
        //    {
        //        models = reqDtls.GetExpFromProject(ddlBgmProjOperSeq1.SelectedValue
        //            , ddlBgmSourceSeq1.SelectedValue, ddlBgmExpGrpSeqNew.SelectedValue, null);
        //    }
        //    else
        //    {
        //        models = new List<FIM_MoneyRequestDtls.M_FIM_MoneyRequestDtls>();
        //    }

        //    grdMoneyRequestDtlsNew.DataSource = models; //ViewState["ProjectExpInfo"];

        //    GridGroupByExpression group1 = new GridGroupByExpression(grdMoneyRequestDtlsNew.Columns.FindByUniqueName("m_bgm_projectoperationexpense.m_bgm_projectexpense.m_bgm_mas_expense.m_bgm_mas_expense_upper.bgm_exp_name"));
        //    GridGroupByExpression group2 = new GridGroupByExpression(grdMoneyRequestDtlsNew.Columns.FindByUniqueName("m_bgm_projectoperationexpense.m_bgm_projectexpense.m_bgm_mas_expense.m_bgm_mas_expense_upper.m_bgm_mas_expense_upper.bgm_exp_name"));

        //    grdMoneyRequestDtlsNew.MasterTableView.GroupByExpressions.Add(group2);
        //    grdMoneyRequestDtlsNew.MasterTableView.GroupByExpressions.Add(group1);
        //    grdMoneyRequestDtlsNew.DataBind();

        //    double dProjOperRemainAmount = 0;
        //    foreach (FIM_MoneyRequestDtls.M_FIM_MoneyRequestDtls model in models)
        //    {
        //        dProjOperRemainAmount += (model.m_bgm_projectoperationexpense.remain_amount ?? 0) - (model.amount ?? 0);
        //    }

        //    lblProjOperRemainAmount.Text = dProjOperRemainAmount.ToString("#,##0.00");
        //}

        protected void LoadFinData()
        {
            if (lblMoneyReqSeq.Text.Trim() != "")
            {
                string strMoneyReqSeq = lblMoneyReqSeq.Text;

                FIM_MoneyRequests.M_FIM_MoneyRequests info = req.GetInfo(strMoneyReqSeq);

                rblFilterEventSeq.SelectedValue = info.event_seq.ToString();

                lblFinRefNo.Text = main.FinString(info.ref_no);
                lblFinMoneyReqDate.Text = main.FinString(txtMoneyReqDate.GetStringValue());
                //if (lblMoneyReqDate.Text != "")
                //{
                //    lblFinMoneyReqDate.Text = main.FinString(lblMoneyReqDate.Text);
                //}
                //else
                //{
                //    lblFinMoneyReqDate.Text = main.FinString(txtMoneyReqDate.GetStringValue());
                //    //lblFinMoneyReqDate.Text = main.FinString(lblMoneyReqDate1.Text);
                //}
                lblFinMoneyReqName.Text = main.FinString(txtMoneyReqName.Text);
                lblFinBgmYear.Text = main.FinString(txtBgmYear.Text);
                lblFinBgmSourceName.Text = main.FinString(ddlBgmSourceSeq.FindItemByValue(ddlBgmSourceSeq.SelectedValue).Text);
                lblFinBgmExpGrpName.Text = main.FinString(rblBgmExpGrpSeq.SelectedItem.Text);
                lblRecordStatus.Text = main.FinString(info.record_status);
                //lblRecordStatus1.Text = main.FinString(info.record_status);
                tableSendBackRemark.Visible = false;
                lblSendBackRemark.Text = "";

                if (rblEventSeq.SelectedValue == "16")
                {
                    panelFinTraveler.Visible = li3.Visible;
                    PanelFinBorrow.Visible = true;
                    PanelFinDocRefNo.Visible = false;
                    lblFinBorrowTypeName.Text = main.FinString(rblBorrowTypeSeq.SelectedItem.Text);
                    lblFinBorrowStartDate.Text = main.FinString(dpBorrowStartDate.GetStringValue());
                    lblFinBorrowEndDate.Text = main.FinString(dpBorrowEndDate.GetStringValue());
                    lblFinBorrowReturnDate.Text = main.FinString(lblBorrowReturnDate.Text);
                }
                else
                {
                    DataTable dt = new DataTable();
                    D_FimMoneyRequest fm = new D_FimMoneyRequest();
                    panelFinTraveler.Visible = false;
                    PanelFinBorrow.Visible = false;
                    PanelFinDocRefNo.Visible = true;
                    PanelContactShow.Visible = true;
                    lblFinDocRefNo.Text = main.FinString(txtDocRefNo.Text);
                    dt = fm.GetcontactName(Convert.ToInt32(strMoneyReqSeq));
                    if (dt.Rows.Count > 0)
                    {

                        DataRow dr = dt.Rows[0];
                        lblContactShow.Text = main.FinString(dr["ConName"].ToString());
                    }
                    else
                    {
                        lblContactShow.Text = " - ";
                    }
                }

                switch (lblRecordStatus.Text)
                {
                    case "W":
                        lblFinRecordStatusName.Text = "ดำเนินการอยู่";
                        lblFinRecordStatusName.ForeColor = System.Drawing.Color.Red;
                        cmdSendMessage1.Visible = true;

                        break;
                    case "P":
                        lblFinRecordStatusName.Text = "รอการอนุมัติโดย " + send.GetLatestMessageStatus(strMoneyReqSeq, "fim_money_requests").to_user_name;
                        lblFinRecordStatusName.ForeColor = System.Drawing.Color.Blue;
                        cmdSendMessage1.Visible = false;

                        break;
                    case "A":
                        lblFinRecordStatusName.Text = "อนุมัติโดย " + send.GetLatestMessageStatus(strMoneyReqSeq, "fim_money_requests").to_user_name;
                        lblFinRecordStatusName.ForeColor = System.Drawing.Color.Green;
                        cmdSendMessage1.Visible = false;

                        break;
                    case "R":
                        lblFinRecordStatusName.Text = "ตีกลับโดย " + send.GetLatestMessageStatus(strMoneyReqSeq, "fim_money_requests").to_user_name;
                        lblFinRecordStatusName.ForeColor = System.Drawing.Color.Red;
                        cmdSendMessage1.Visible = true;
                        tableSendBackRemark.Visible = true;

                        var replys = reply.GetInfoFromRefSeq(strMoneyReqSeq, "fim_money_requests");

                        if (replys.Count > 0)
                        {
                            lblSendBackRemark.Text = replys[0].remarks;
                        }

                        break;
                    default:
                        lblFinRecordStatusName.Text = "รอการยืนยันโดย " + send.GetLatestMessageStatus(strMoneyReqSeq, "fim_money_requests").to_user_name;
                        lblFinRecordStatusName.ForeColor = System.Drawing.Color.Red;
                        cmdSendMessage1.Visible = true;

                        break;
                }

                //switch (lblRecordStatus1.Text)
                //{
                //    case "W":
                //        lblFinRecordStatusName.Text = "ดำเนินการอยู่";
                //        lblFinRecordStatusName.ForeColor = System.Drawing.Color.Red;
                //        cmdSendMessage1.Visible = true;

                //        break;
                //    case "P":
                //        lblFinRecordStatusName.Text = "รอการอนุมัติโดย " + send.GetLatestMessageStatus(strMoneyReqSeq, "fim_money_requests").to_user_name;
                //        lblFinRecordStatusName.ForeColor = System.Drawing.Color.Blue;
                //        cmdSendMessage1.Visible = false;

                //        break;
                //    case "A":
                //        lblFinRecordStatusName.Text = "อนุมัติโดย " + send.GetLatestMessageStatus(strMoneyReqSeq, "fim_money_requests").to_user_name;
                //        lblFinRecordStatusName.ForeColor = System.Drawing.Color.Green;
                //        cmdSendMessage1.Visible = false;

                //        break;
                //    case "R":
                //        lblFinRecordStatusName.Text = "ตีกลับโดย " + send.GetLatestMessageStatus(strMoneyReqSeq, "fim_money_requests").to_user_name;
                //        lblFinRecordStatusName.ForeColor = System.Drawing.Color.Red;
                //        cmdSendMessage1.Visible = true;
                //        tableSendBackRemark.Visible = true;
                //        lblSendBackRemark.Text = reply.GetInfoFromRefSeq(strMoneyReqSeq, "fim_money_requests")[0].remarks;

                //        break;
                //    default:
                //        lblFinRecordStatusName.Text = "รอการยืนยันโดย " + send.GetLatestMessageStatus(strMoneyReqSeq, "fim_money_requests").to_user_name;
                //        lblFinRecordStatusName.ForeColor = System.Drawing.Color.Red;
                //        cmdSendMessage1.Visible = true;

                //        break;
                //}

                LoadFinMoneyReqDtls();

                IList<FIM_MoneyRequestTravelers.M_FIM_MoneyRequestTravelers> personInfoList = reqTrav.GetFull(strMoneyReqSeq);
                Repeater3.DataSource = personInfoList;
                Repeater3.DataBind();
            }
        }

        protected void LoadFinMoneyReqDtls()
        {
            double dSumAmount = 0;
            double dSumAmount1 = 0;
            double dSumAmount2 = 0;
            double dSumAmountTax = 0;
            double dSumAmountTax1 = 0;
            double dSumAmountTax2 = 0;
            double dSumAmountVat = 0;
            double dSumAmountVat1 = 0;
            double dSumAmountVat2 = 0;
            int nOldBgmExpSeq1 = 0;
            int nOldBgmExpSeq2 = 0;
            IList<FIM_MoneyRequestDtls.M_FIM_MoneyRequestDtls> masterListInfo
                = reqDtls.GetFull(lblMoneyReqSeq.Text)
                .OrderBy(m => m.m_bgm_projectoperationexpense.m_bgm_projectexpense.m_bgm_mas_expense.m_bgm_mas_expense_upper.bgm_exp_seq)
                .OrderBy(m => m.m_bgm_projectoperationexpense.m_bgm_projectexpense.m_bgm_mas_expense.m_bgm_mas_expense_upper.m_bgm_mas_expense_upper.bgm_exp_seq).ToList();

            DataTable dtbExp2 = new DataTable();
            dtbExp2.Columns.Add("bgm_exp_seq", typeof(Int32));
            dtbExp2.Columns.Add("bgm_exp_name", typeof(String));
            dtbExp2.Columns.Add("amount", typeof(Double));
            dtbExp2.Columns.Add("amount_vat", typeof(Double));
            dtbExp2.Columns.Add("amount_tax", typeof(Double));

            DataTable dtbExp1 = new DataTable();
            dtbExp1.Columns.Add("upper_bgm_exp_seq", typeof(Int32));
            dtbExp1.Columns.Add("bgm_exp_seq", typeof(Int32));
            dtbExp1.Columns.Add("bgm_exp_name", typeof(String));
            dtbExp1.Columns.Add("amount", typeof(Double));
            dtbExp1.Columns.Add("amount_vat", typeof(Double));
            dtbExp1.Columns.Add("amount_tax", typeof(Double));

            DataTable dtbExp = new DataTable();
            dtbExp.Columns.Add("upper_bgm_exp_seq", typeof(Int32));
            dtbExp.Columns.Add("bgm_exp_seq", typeof(Int32));
            dtbExp.Columns.Add("bgm_exp_name", typeof(String));
            dtbExp.Columns.Add("remarks", typeof(String));
            dtbExp.Columns.Add("amount", typeof(Double));
            dtbExp.Columns.Add("amount_vat", typeof(Double));
            dtbExp.Columns.Add("amount_tax", typeof(Double));

            for (int i = 0; i < masterListInfo.Count; i++)
            {
                FIM_MoneyRequestDtls.M_FIM_MoneyRequestDtls info = masterListInfo[i];
                DataRow dr = dtbExp.NewRow();
                double dAmount = info.amount ?? 0;
                double dAmountVat = info.amount_vat ?? 0;
                double dAmountTax = info.amount_tax ?? 0;

                if (dAmount > 0)
                {
                    dr["upper_bgm_exp_seq"] = info.m_bgm_projectoperationexpense.m_bgm_projectexpense.m_bgm_mas_expense.m_bgm_mas_expense_upper.bgm_exp_seq;
                    dr["bgm_exp_seq"] = info.m_bgm_projectoperationexpense.m_bgm_projectexpense.bgm_exp_seq;
                    dr["bgm_exp_name"] = info.m_bgm_projectoperationexpense.m_bgm_projectexpense.m_bgm_mas_expense.bgm_exp_name;
                    dr["remarks"] = info.remarks;
                    dr["amount"] = dAmount;
                    dr["amount_vat"] = dAmountVat;
                    dr["amount_tax"] = dAmountTax;
                    dtbExp.Rows.Add(dr);
                    dSumAmount += dAmount;
                    dSumAmountVat += dAmountVat;
                    dSumAmountTax += dAmountTax;
                    // exp1 ======================================================================================
                    if (info.m_bgm_projectoperationexpense.m_bgm_projectexpense.m_bgm_mas_expense.m_bgm_mas_expense_upper.bgm_exp_seq == nOldBgmExpSeq1)
                    {
                        dSumAmount1 += dAmount;
                        dSumAmountVat1 += dAmountVat;
                        dSumAmountTax1 += dAmountTax;
                    }
                    else
                    {
                        DataRow dr1 = dtbExp1.NewRow();
                        dr1["upper_bgm_exp_seq"] = info.m_bgm_projectoperationexpense.m_bgm_projectexpense.m_bgm_mas_expense.m_bgm_mas_expense_upper.m_bgm_mas_expense_upper.bgm_exp_seq;
                        dr1["bgm_exp_seq"] = info.m_bgm_projectoperationexpense.m_bgm_projectexpense.m_bgm_mas_expense.m_bgm_mas_expense_upper.bgm_exp_seq;
                        dr1["bgm_exp_name"] = info.m_bgm_projectoperationexpense.m_bgm_projectexpense.m_bgm_mas_expense.m_bgm_mas_expense_upper.bgm_exp_name;

                        if (dtbExp1.Rows.Count > 0)
                        {
                            dtbExp1.Rows[dtbExp1.Rows.Count - 1]["amount"] = dSumAmount1;
                            dtbExp1.Rows[dtbExp1.Rows.Count - 1]["amount_vat"] = dSumAmountVat1;
                            dtbExp1.Rows[dtbExp1.Rows.Count - 1]["amount_tax"] = dSumAmountTax1;
                        }

                        dSumAmount1 = dAmount;
                        dSumAmountVat1 = dAmountVat;
                        dSumAmountTax1 = dAmountTax;
                        nOldBgmExpSeq1 = info.m_bgm_projectoperationexpense.m_bgm_projectexpense.m_bgm_mas_expense.m_bgm_mas_expense_upper.bgm_exp_seq.Value;

                        dtbExp1.Rows.Add(dr1);
                    }
                    // exp2 ======================================================================================
                    if (info.m_bgm_projectoperationexpense.m_bgm_projectexpense.m_bgm_mas_expense.m_bgm_mas_expense_upper.m_bgm_mas_expense_upper.bgm_exp_seq == nOldBgmExpSeq2)
                    {
                        dSumAmount2 += dAmount;
                        dSumAmountVat2 += dAmountVat;
                        dSumAmountTax2 += dAmountTax;
                    }
                    else
                    {
                        DataRow dr2 = dtbExp2.NewRow();
                        dr2["bgm_exp_seq"] = info.m_bgm_projectoperationexpense.m_bgm_projectexpense.m_bgm_mas_expense.m_bgm_mas_expense_upper.m_bgm_mas_expense_upper.bgm_exp_seq;
                        dr2["bgm_exp_name"] = info.m_bgm_projectoperationexpense.m_bgm_projectexpense.m_bgm_mas_expense.m_bgm_mas_expense_upper.m_bgm_mas_expense_upper.bgm_exp_name;

                        if (dtbExp2.Rows.Count > 0)
                        {
                            dtbExp2.Rows[dtbExp2.Rows.Count - 1]["amount"] = dSumAmount2;
                            dtbExp2.Rows[dtbExp2.Rows.Count - 1]["amount_vat"] = dSumAmountVat2;
                            dtbExp2.Rows[dtbExp2.Rows.Count - 1]["amount_tax"] = dSumAmountTax2;
                        }

                        dSumAmount2 = dAmount;
                        dSumAmountVat2 = dAmountVat;
                        dSumAmountTax2 = dAmountTax;
                        nOldBgmExpSeq2 = info.m_bgm_projectoperationexpense.m_bgm_projectexpense.m_bgm_mas_expense.m_bgm_mas_expense_upper.m_bgm_mas_expense_upper.bgm_exp_seq.Value;

                        dtbExp2.Rows.Add(dr2);
                    }
                }

                if (i == masterListInfo.Count - 1 && dtbExp2.Rows.Count > 0)
                {
                    dtbExp2.Rows[dtbExp2.Rows.Count - 1]["amount"] = dSumAmount2;
                    dtbExp2.Rows[dtbExp2.Rows.Count - 1]["amount_vat"] = dSumAmountVat2;
                    dtbExp2.Rows[dtbExp2.Rows.Count - 1]["amount_tax"] = dSumAmountTax2;
                    dtbExp1.Rows[dtbExp1.Rows.Count - 1]["amount"] = dSumAmount1;
                    dtbExp1.Rows[dtbExp1.Rows.Count - 1]["amount_vat"] = dSumAmountVat1;
                    dtbExp1.Rows[dtbExp1.Rows.Count - 1]["amount_tax"] = dSumAmountTax1;
                }
            }

            RepeaterBgmExp2.DataSource = dtbExp2;
            RepeaterBgmExp2.DataBind();

            for (int k = 0; k < RepeaterBgmExp2.Items.Count; k++)
            {
                Repeater RepeaterBgmExp1 = (Repeater)RepeaterBgmExp2.Items[k].FindControl("RepeaterBgmExp1");
                Label lblBgmExpSeq = (Label)RepeaterBgmExp2.Items[k].FindControl("lblBgmExpSeq");

                DataTable dtbExp1Copy = dtbExp1.Copy();
                dtbExp1Copy.Rows.Clear();
                DataRow[] drc3 = dtbExp1.Select("upper_bgm_exp_seq = " + lblBgmExpSeq.Text);
                foreach (DataRow dr in drc3)
                {
                    DataRow drCopy = dr;
                    dtbExp1Copy.ImportRow(drCopy);
                }

                RepeaterBgmExp1.DataSource = dtbExp1Copy;
                RepeaterBgmExp1.DataBind();

                for (int m = 0; m < RepeaterBgmExp1.Items.Count; m++)
                {
                    Repeater RepeaterBgmExp = (Repeater)RepeaterBgmExp1.Items[m].FindControl("RepeaterBgmExp");
                    Label lblBgmExpSeq2 = (Label)RepeaterBgmExp1.Items[m].FindControl("lblBgmExpSeq");

                    DataTable dtbExpCopy = dtbExp.Copy();
                    dtbExpCopy.Rows.Clear();
                    DataRow[] drc4 = dtbExp.Select("upper_bgm_exp_seq = " + lblBgmExpSeq2.Text);
                    foreach (DataRow dr in drc4)
                    {
                        DataRow drCopy = dr;
                        dtbExpCopy.ImportRow(drCopy);
                    }

                    RepeaterBgmExp.DataSource = dtbExpCopy;
                    RepeaterBgmExp.DataBind();
                }
            }

            lblFinSumAmount.Text = dSumAmount.ToString("#,###.00");
            lblFinSumAmountVat.Text = dSumAmountVat.ToString("#,###.00");
            lblFinSumAmountTax.Text = dSumAmountTax.ToString("#,###.00");
        }

        /*
        protected void SetQty(string strBgmExpSeq)
        {
            ViewState["bRelate"] = rateType.GetRelate(strBgmExpSeq);
        }        

        protected void LoadRepeaterFilter()
        {
            string strBgmExpSeq = lblBgmExpSeq.Text;

            RepeaterFilter.DataSource = rateTypeGrp.GetFull(strBgmExpSeq);
            RepeaterFilter.DataBind();
        }
        */

        protected void LoadRepeaterExpRate()
        {
            string strMoneyReqDtlsSeq = lblMoneyReqDtlsSeq.Text;
            string strBgmProjOperExpSeq = lblBgmProjOperExpSeq.Text;
            //string strBgmSourceSeq = lblBgmSourceSeq.Text;
            string strSeq;
            //string[] strExpRateTypeSeq = new string[RepeaterFilter.Items.Count];
            //int j = 0;

            //for (int i = 0; i < strExpRateTypeSeq.Length; i++)
            //{
            //    RadComboBox ddl = (RadComboBox)RepeaterFilter.Items[i].FindControl("ddlExpRateTypeSeq");
            //    strSeq = ddl.SelectedValue;

            //    if (strSeq.Length > 0)
            //    {
            //        strExpRateTypeSeq[j] = strSeq;
            //        j++;
            //    }
            //}

            if (strMoneyReqDtlsSeq.Length > 0)
            {
                RepeaterExpRate.DataSource = reqRate.GetFull(strMoneyReqDtlsSeq, null);
            }
            else
            {
                RepeaterExpRate.DataSource = reqRate.GetFull2(strBgmProjOperExpSeq, null);
            }

            RepeaterExpRate.DataBind();
        }

        protected void LoadRepeaterExpRateEmp()
        {
            string strMoneyReqSeq = lblMoneyReqSeq.Text;
            //string strMoneyReqDtlsSeq = lblMoneyReqDtlsSeq.Text;
            //string strBgmProjOperExpSeq = lblBgmProjOperExpSeq.Text;
            //string strBgmSourceSeq = ddlBgmSourceSeq.SelectedValue;
            //string strSeq;

            RepeaterExpRateEmp.DataSource = reqTrav.GetFull(strMoneyReqSeq);
            RepeaterExpRateEmp.DataBind();
        }

        protected void RepeaterExpRate_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item is RepeaterItem)
            {
                if (e.Item.ItemType != ListItemType.Header)
                {
                    RepeaterItem dataItem = e.Item;
                    RadNumericTextBox txtMan = (RadNumericTextBox)dataItem.FindControl("txtMan");
                    RadNumericTextBox txtDay = (RadNumericTextBox)dataItem.FindControl("txtDay");
                    RadNumericTextBox txtHour = (RadNumericTextBox)dataItem.FindControl("txtHour");
                    RadNumericTextBox txtMinute = (RadNumericTextBox)dataItem.FindControl("txtMinute");
                    Label lblExpRateName = (Label)dataItem.FindControl("lblExpRateName");
                    Label lblExpRateDescr = (Label)dataItem.FindControl("lblExpRateDescr");
                    Label lblMan = (Label)dataItem.FindControl("lblMan");
                    Label lblDay = (Label)dataItem.FindControl("lblDay");
                    Label lblHour = (Label)dataItem.FindControl("lblHour");
                    Label lblMinute = (Label)dataItem.FindControl("lblMinute");
                    Label lblRelateMan = (Label)dataItem.FindControl("lblRelateMan");
                    Label lblRelateDay = (Label)dataItem.FindControl("lblRelateDay");

                    bool bRelateMan = lblRelateMan.Text.ToLower() == "true";
                    bool bRelateDay = lblRelateDay.Text.ToLower() == "true";

                    lblExpRateName.ToolTip = lblExpRateDescr.Text;

                    if (bRelateMan)
                    {
                        txtMan.Visible = true;
                        lblMan.Visible = true;
                    }

                    if (bRelateDay)
                    {
                        txtDay.Visible = true;
                        lblDay.Visible = true;
                    }

                    //if (bRelate[2])
                    //{
                    //    txtHour.Visible = true;
                    //    lblHour.Visible = true;
                    //}

                    //if (bRelate[3])
                    //{
                    //    txtMinute.Visible = true;
                    //    lblMinute.Visible = true;
                    //}
                }
            }
        }

        protected void RepeaterExpRateEmp_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item is RepeaterItem)
            {
                if (e.Item.ItemType != ListItemType.Header)
                {
                    RepeaterItem item = e.Item;
                    Label lblEmpSeq = (Label)item.FindControl("lblEmpSeq");
                    string strMoneyReqDtlsSeq = lblMoneyReqDtlsSeq.Text;
                    string strBgmProjOperExpSeq = lblBgmProjOperExpSeq.Text;
                    string strBgmSourceSeq = ddlBgmSourceSeq.SelectedValue;
                    string strBgmExpSeq = lblBgmExpSeq.Text;
                    FIM_MoneyRequestDtlsRates.M_FIM_MoneyRequestDtlsRates rateInfo = reqRate.GetInfo2(strMoneyReqDtlsSeq, lblEmpSeq.Text);

                    Label lblClassSeq = (Label)item.FindControl("lblClassSeq");
                    Label lblMoneyReqDtlsRateSeq = (Label)item.FindControl("lblMoneyReqDtlsRateSeq");
                    RadComboBox ddlExpRateSeq = (RadComboBox)item.FindControl("ddlExpRateSeq");
                    RadNumericTextBox txtAmount = (RadNumericTextBox)item.FindControl("txtAmount");
                    RadNumericTextBox txtMan = (RadNumericTextBox)item.FindControl("txtMan");
                    RadNumericTextBox txtDay = (RadNumericTextBox)item.FindControl("txtDay");
                    RadNumericTextBox txtHour = (RadNumericTextBox)item.FindControl("txtHour");
                    RadNumericTextBox txtMinute = (RadNumericTextBox)item.FindControl("txtMinute");
                    Label lblTotalAmount = (Label)item.FindControl("lblTotalAmount");
                    Label lblMan = (Label)item.FindControl("lblMan");
                    Label lblDay = (Label)item.FindControl("lblDay");
                    Label lblHour = (Label)item.FindControl("lblHour");
                    Label lblMinute = (Label)item.FindControl("lblMinute");
                    HtmlInputHidden hAmountMin = (HtmlInputHidden)item.FindControl("hAmountMin");
                    HtmlInputHidden hAmountMax = (HtmlInputHidden)item.FindControl("hAmountMax");
                    LinkButton cmdAmountMax = (LinkButton)item.FindControl("cmdAmountMax");

                    //bool[] bRelate = rateType.GetRelate(strBgmExpSeq);

                    //if (bRelate[0])
                    //{
                    //    txtMan.Visible = true;
                    //    lblMan.Visible = true;
                    //}

                    //if (bRelate[1])
                    //{
                    //    txtDay.Visible = true;
                    //    lblDay.Visible = true;
                    //}

                    //if (bRelate[2])
                    //{
                    //    txtHour.Visible = true;
                    //    lblHour.Visible = true;
                    //}

                    //if (bRelate[3])
                    //{
                    //    txtMinute.Visible = true;
                    //    lblMinute.Visible = true;
                    //}

                    ddlExpRateSeq.DataSource = exprate.GetFull(strBgmExpSeq, strBgmSourceSeq);
                    //ddlExpRateSeq.DataSource = rateType.GetClassExpRateList(lblClassSeq.Text, strBgmSourceSeq, strBgmExpSeq);
                    ddlExpRateSeq.DataBind();

                    if (rateInfo == null)
                    {
                        ddlExpRateSeq.SelectedIndex = 0;
                        ddlExpRateSeq_SelectedIndexChanged(ddlExpRateSeq, null);
                        txtMan.Text = "1";
                        txtDay.Text = "1";
                        txtHour.Text = "1";
                        txtMinute.Text = "1";
                    }
                    else
                    {
                        if (rateInfo.money_req_dtls_rate_seq == null)
                        {
                            ddlExpRateSeq.SelectedIndex = 0;
                            ddlExpRateSeq_SelectedIndexChanged(ddlExpRateSeq, null);
                            txtMan.Text = "1";
                            txtDay.Text = "1";
                            txtHour.Text = "1";
                            txtMinute.Text = "1";
                        }
                        else
                        {
                            lblMoneyReqDtlsRateSeq.Text = rateInfo.money_req_dtls_rate_seq.ToString();
                            ddlExpRateSeq.SelectedValue = rateInfo.exp_rate_seq.ToString();
                            ddlExpRateSeq_SelectedIndexChanged(ddlExpRateSeq, null);
                            txtAmount.Text = rateInfo.amount.Value.ToString("#,###.00");
                            txtMan.Text = rateInfo.man.ToString();
                            txtDay.Text = rateInfo.day.ToString();
                            txtHour.Text = rateInfo.hour.ToString();
                            txtMinute.Text = rateInfo.minute.ToString();
                            lblTotalAmount.Text = rateInfo.total_amount.Value.ToString("#,###.00");
                        }
                    }
                }
            }
        }

        protected void ddlExpRateTypeSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadRepeaterExpRate();
        }

        protected void txtFilterBgmYear_TextChanged(object sender, EventArgs e)
        {
            LoadGrdViewAll();
        }

        protected void ddlExpRateSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            RadComboBox ddlExpRateSeq = (RadComboBox)o;
            RepeaterItem item = (RepeaterItem)ddlExpRateSeq.Parent;
            HtmlInputHidden hAmountMin = (HtmlInputHidden)item.FindControl("hAmountMin");
            HtmlInputHidden hAmountMax = (HtmlInputHidden)item.FindControl("hAmountMax");
            LinkButton cmdAmountMax = (LinkButton)item.FindControl("cmdAmountMax");
            string strExpRateSeq = ddlExpRateSeq.SelectedValue;
            FIM_MAS_ExpenseRates.M_FIM_MAS_ExpenseRates info = exprate.GetInfo(strExpRateSeq);

            hAmountMin.Value = info.amount_min.ToString();
            hAmountMax.Value = info.amount_max.ToString();
            cmdAmountMax.Text = "ไม่เกิน " + info.amount_max.ToString();
        }

        protected void txtAmount_TextChanged(object sender, EventArgs e)
        {
            CalculateTotalAmount(sender);
        }

        protected void txtMan_TextChanged(object sender, EventArgs e)
        {
            CalculateTotalAmount(sender);
        }

        protected void txtDay_TextChanged(object sender, EventArgs e)
        {
            CalculateTotalAmount(sender);
        }

        protected void txtHour_TextChanged(object sender, EventArgs e)
        {
            CalculateTotalAmount(sender);
        }

        protected void txtMinute_TextChanged(object sender, EventArgs e)
        {
            CalculateTotalAmount(sender);
        }

        protected void CalculateTotalAmount(object sender)
        {
            RepeaterItem item = null;

            if (sender.GetType().Name == "RadNumericTextBox")
            {
                item = (RepeaterItem)((RadNumericTextBox)sender).Parent;
            }
            else if (sender.GetType().Name == "LinkButton")
            {
                item = (RepeaterItem)((LinkButton)sender).Parent;
            }

            HtmlInputHidden hAmountMin = (HtmlInputHidden)item.FindControl("hAmountMin");
            HtmlInputHidden hAmountMax = (HtmlInputHidden)item.FindControl("hAmountMax");
            RadNumericTextBox txtAmount = (RadNumericTextBox)item.FindControl("txtAmount");
            double dAmount = (txtAmount.Value != null ? txtAmount.Value.Value : 1);
            double dMin = (main.WebString(hAmountMin.Value) != "" ? Convert.ToDouble(hAmountMin.Value) : 0);
            double dMax = (main.WebString(hAmountMax.Value) != "" ? Convert.ToDouble(hAmountMax.Value) : 0);

            if (dAmount > dMax)
            {
                txtAmount.Text = dMax.ToString("#,###.00");
                main.MessageAlert(Page, "จำนวนเงินมากกว่าอัตราค่าใช้จ่าย");
                return;
            }

            if (dAmount < dMin)
            {
                txtAmount.Text = dMin.ToString("#,###.00");
                main.MessageAlert(Page, "จำนวนเงินน้อยกว่าอัตราค่าใช้จ่าย");
                return;
            }

            RadNumericTextBox txtMan = (RadNumericTextBox)item.FindControl("txtMan");
            RadNumericTextBox txtDay = (RadNumericTextBox)item.FindControl("txtDay");
            RadNumericTextBox txtHour = (RadNumericTextBox)item.FindControl("txtHour");
            RadNumericTextBox txtMinute = (RadNumericTextBox)item.FindControl("txtMinute");
            Label lblTotalAmount = (Label)item.FindControl("lblTotalAmount");
            double dTotalAmount = dAmount
                * (txtMan.Value != null ? txtMan.Value.Value : 1)
                * (txtDay.Value != null ? txtDay.Value.Value : 1)
                * (txtHour.Value != null ? txtHour.Value.Value : 1)
                * (txtMinute.Value != null ? txtMinute.Value.Value : 1);

            lblTotalAmount.Text = dTotalAmount.ToString("#,###.00");
            CalculateSumAmount();
        }

        protected void CalculateSumAmount()
        {
            double dSumAmount = 0;

            if (RepeaterExpRate.Visible)
            {
                foreach (RepeaterItem item in RepeaterExpRate.Items)
                {
                    Label lblTotalAmount = (Label)item.FindControl("lblTotalAmount");
                    dSumAmount = dSumAmount + (lblTotalAmount.Text.Length > 0 ? Convert.ToDouble(lblTotalAmount.Text) : 0);
                }
            }
            else
            {
                foreach (RepeaterItem item in RepeaterExpRateEmp.Items)
                {
                    Label lblTotalAmount = (Label)item.FindControl("lblTotalAmount");
                    dSumAmount = dSumAmount + (lblTotalAmount.Text.Length > 0 ? Convert.ToDouble(lblTotalAmount.Text) : 0);
                }
            }

            txtAmount.Text = dSumAmount.ToString("#,###.00");
            //txtAmount2_TextChanged(null, null);
        }

        protected void LoadRepeaterPerson()
        {
            string strMoneyReqSeq = lblMoneyReqSeq.Text;

            IList<FIM_MoneyRequestTravelers.M_FIM_MoneyRequestTravelers> infoList = reqTrav.GetFull(strMoneyReqSeq);
            RepeaterPerson.DataSource = infoList;
            RepeaterPerson.DataBind();
        }

        protected bool CheckData1()
        {
            bool bOK = true;
            string strError = "";
            string strJobsiteSeq = lblWorkingJobSiteSeq.Text;
            string strWorkAreaSeq = lblWorkAreaSeq.Text;
            string strMoneyReqDate = txtMoneyReqDate.GetStringValue();//lblMoneyReqDate.Text;
            string strBgmYear = txtBgmYear.Text;
            //string strBgmYear1 = txtBgmYear1.Text;
            string strBgmSourceSeq = ddlBgmSourceSeq.SelectedValue;
            //string strBgmExpGrpSeq = rblBgmExpGrpSeq.SelectedValue;
            string strBgmProjOperSeq = ddlBgmProjOperSeq.SelectedValue;
            string strMoneyReqName = txtMoneyReqName.Text;
            string strEventSeq = rblEventSeq.SelectedValue;

            if (strBgmYear.Length == 0)
            {
                bOK = false;
                strError = main.ErrorEmpty("ปีงบประมาณ");
            }
            //else if (strBgmYear1.Length == 0)
            //{
            //    bOK = false;
            //    strError = main.ErrorEmpty("ปีงบประมาณ");
            //}
            else if (strBgmProjOperSeq.Length == 0 && strEventSeq != "17")
            {
                bOK = false;
                strError = main.ErrorEmpty("อนุมัติโครงการ");
            }
            else if (strMoneyReqName.Length == 0)
            {
                bOK = false;
                strError = main.ErrorEmpty("ชื่อรายการ");
            }
            else if (strJobsiteSeq.Length == 0)
            {
                bOK = false;
                strError = main.ErrorEmpty("หน่วยงานที่สังกัด");
            }
            else if (strBgmSourceSeq.Length == 0)
            {
                bOK = false;
                strError = main.ErrorEmpty("แหล่งเงิน");
            }
            //else if (strBgmExpGrpSeq.Length == 0)
            //{
            //    bOK = false;
            //    strError = main.ErrorEmpty("งบรายจ่าย");
            //}
            else if (strEventSeq.Length == 0)
            {
                bOK = false;
                strError = main.ErrorEmpty("ประเภทรายการ");
            }
            else if (strWorkAreaSeq.Length == 0)
            {
                bOK = false;
                strError = main.ErrorEmpty("หน่วยงานที่สังกัด(เขตพื้นที่)");
            }
            else if (strEventSeq == "16")
            {
                string strBorrowTypeSeq = rblBorrowTypeSeq.SelectedValue;
                string strBorrowStartDate = dpBorrowStartDate.GetStringValue();
                string strBorrowEndDate = dpBorrowEndDate.GetStringValue();
                //string strBorrowReturn = lblBorrowReturnDate.Text;

                if (strBorrowTypeSeq.Length == 0)
                {
                    bOK = false;
                    strError = main.ErrorEmpty("วัตถุประสงค์ในการยืม");
                }
                else if (strBorrowStartDate.Length == 0)
                {
                    bOK = false;
                    strError = main.ErrorEmpty("วันที่เริ่ม");
                }
                else if (strBorrowEndDate.Length == 0)
                {
                    bOK = false;
                    strError = main.ErrorEmpty("วันที่สิ้นสุด");
                }
                else if (dpBorrowStartDate.GetDateValue() > dpBorrowEndDate.GetDateValue())
                {
                    bOK = false;
                    strError = "วันที่เริ่มต้นไม่สัมพันธ์กับวันที่สิ้นสุด";
                }
            }
            else if (strEventSeq == "12" && rblPayeeType.SelectedValue == "2")
            {
                //if (ddlContactSeq.SelectedValue.Length == 0)
                //{
                //    bOK = false;
                //    strError = main.ErrorEmpty("ชื่อบุคคลภายนอก");
                //}

                if (ddlContactSeq.SelectedValue.Length == 0)
                {
                    bOK = false;
                    strError = main.ErrorEmpty("ชื่อบุคคลภายนอก");
                }
            }

            if (!bOK)
                main.MessageAlert(Page, strError);

            return bOK;
        }

        protected bool CheckData2()
        {
            bool bOK = true;
            string strError = "";
            string strAmount = txtAmount.Text;

            if (strAmount.Length == 0)
            {
                bOK = false;
                strError = main.ErrorEmpty("จำนวนเงินที่ขอ");
            }
            else if (chkManual.Visible && chkManual.Checked && txtRemarks.Text.Trim().Length == 0)
            {
                bOK = false;
                strError = main.ErrorEmpty("กรุณากรอกรายละเอียดของจำนวนเงินที่ขอด้วย");
            }
            else if (chkIncludeVat.Checked && (string.IsNullOrEmpty(ddlVender.SelectedValue)
                || string.IsNullOrEmpty(txtBranchNo.Text) || string.IsNullOrEmpty(txtDtlsRefNo.Text)
                || txtDtlsRefDate.GetDateValue() == null))
            {
                bOK = false;
                strError = main.ErrorEmpty("กรุณากรอกรายละเอียดของใบกำกับภาษีให้ครบถ้วน");
            }

            if (!bOK)
                main.MessageAlert(Page, strError);

            return bOK;
        }

        protected bool CheckData3()
        {
            bool bOK = true;
            string strError = "";
            string strEmpSeq = ddlEmpSeq.SelectedValue;

            if (strEmpSeq.Length == 0)
            {
                bOK = false;
                strError = main.ErrorEmpty("ชื่อผู้เดินทาง");
            }

            if (!bOK)
                main.MessageAlert(Page, strError);

            return bOK;
        }

        protected bool CheckAmount(string strRemainAmount, string strAmount)
        {
            double dRemainAmount = (main.WebString(strRemainAmount).Length > 0 ? Convert.ToDouble(strRemainAmount) : 0);
            double dAmount = (strAmount.Length > 0 ? Convert.ToDouble(strAmount) : 0);
            bool bOK = dRemainAmount >= dAmount;

            return bOK;
        }

        protected void InsertMuti()
        {
            string strMoneyReqSeq;
            string strJobsiteSeq = lblWorkingJobSiteSeq.Text;
            string strWorkAreaSeq = lblWorkAreaSeq.Text;
            string strMoneyReqDate = txtMoneyReqDate.GetStringValue();//lblMoneyReqDate.Text;
            string strBgmYear = txtBgmYear.Text;
            string strBgmSourceSeq = ddlBgmSourceSeq.SelectedValue;
            //string strBgmExpGrpSeq = rblBgmExpGrpSeq.SelectedValue;
            string strBgmProjOperSeq = ddlBgmProjOperSeq.SelectedValue;
            string strMoneyReqName = txtMoneyReqName.Text;
            string strEventSeq = rblEventSeq.SelectedValue;
            //string strRefNo = "";
            string strEmpSeq = Session["EmpSeq"].ToString();
            string strContactSeq = //(rblPayeeType.SelectedValue == "2" && strEventSeq == "12" ? null :ddlContactSeq.SelectedValue);
                rblPayeeType.SelectedValue == "2" ? rblPayeeType.SelectedValue : null;
            string strCreditCardNo = null;

            string strBorrowNo = null;
            string strBorrowTypeSeq = null;
            string strBorrowStartDate = null;
            string strBorrowEndDate = null;
            string strBorrowReturnDate = null;
            string strDebtorEmpSeq = null;
            string strTransferTypeSeq = null;

            string strDocRefNo = null;

            string strUserID = strEmpSeq;
            DateTime dtNow = DateTime.Now;
            FIM_MoneyRequests.M_FIM_MoneyRequests info = new FIM_MoneyRequests.M_FIM_MoneyRequests();

            switch (strEventSeq)
            {
                case "16"://เงินยืม
                case "18"://ยืมบัตรเครดิต
                    //strBorrowNo = req.GenerateNextMoneyRequestBorrowNumber();
                    strBorrowTypeSeq = rblBorrowTypeSeq.SelectedValue;
                    strBorrowStartDate = dpBorrowStartDate.GetStringValue();
                    strBorrowEndDate = dpBorrowEndDate.GetStringValue();
                    strDebtorEmpSeq = strUserID;

                    CalReturnDate();
                    strBorrowReturnDate = lblBorrowReturnDate.Text;
                    strCreditCardNo = txtCreditCardNo.Text;
                    break;
                case "12"://เบิกเงิน
                case "17":
                    strDocRefNo = txtDocRefNo.Text;
                    break;
                case "19":
                    strTransferTypeSeq = rblTransferTypeSeq.SelectedValue;
                    strBorrowStartDate = dpBorrowStartDate.GetStringValue();
                    strBorrowEndDate = dpBorrowEndDate.GetStringValue();

                    strBorrowReturnDate = lblBorrowReturnDate.Text;
                    info.transfer_job_site_seq = main.ConvertNullInt(ddlJobSiteSeq.SelectedValue);
                    break;
            }

            info.bgm_source_seq = main.ConvertNullInt(strBgmSourceSeq);
            info.bgm_year = main.ConvertNullInt(strBgmYear);
            info.work_area_seq = main.ConvertNullInt(strWorkAreaSeq);
            //info.bgm_exp_grp_seq = main.ConvertNullInt(strBgmExpGrpSeq);
            info.event_seq = main.ConvertNullInt(strEventSeq);
            info.money_req_date = main.ConvertNullDateTime(strMoneyReqDate);
            info.use_advance = "0";
            info.advance_bgm_source_seq = null;
            string refNo = req.GenerateNextMoneyRequestRefNo(strBgmSourceSeq == "1" ? "1" : "2");
            if (string.IsNullOrEmpty(refNo))
            {
                if (string.IsNullOrEmpty(refNo = req.GenerateNextMoneyRequestRefNo(strBgmSourceSeq == "1" ? "1" : "2")))
                {
                    if (string.IsNullOrEmpty(refNo = req.GenerateNextMoneyRequestRefNo(strBgmSourceSeq == "1" ? "1" : "2")))
                    {
                        throw new Exception("Cannot generate refno (null)");
                    }
                }
            }
            info.ref_no = refNo;
            info.doc_ref_no = strDocRefNo;
            info.money_req_name = strMoneyReqName;
            info.money_req_name_plus = "";
            info.bgm_proj_oper_seq = main.ConvertNullInt(strBgmProjOperSeq);
            info.emp_seq = main.ConvertNullInt(strEmpSeq);
            info.job_site_seq = main.ConvertNullInt(strJobsiteSeq);
            info.contact_seq = main.ConvertNullInt(strContactSeq);
            info.icm_dn_seq = null;
            info.approver_seq = null;
            info.borrow_no = strBorrowNo;
            info.borrow_type_seq = main.ConvertNullInt(strBorrowTypeSeq);
            info.borrow_start_date = main.ConvertNullDateTime(strBorrowStartDate);
            info.borrow_end_date = main.ConvertNullDateTime(strBorrowEndDate);
            info.borrow_return_date = main.ConvertNullDateTime(strBorrowReturnDate);
            info.transfer_type_seq = main.ConvertNullInt(strTransferTypeSeq);
            info.allow = "0";
            info.debtor_emp_seq = main.ConvertNullInt(strDebtorEmpSeq);
            info.credit_card_no = strCreditCardNo;

            info.created_by = main.ConvertNullInt(strUserID);
            info.created_when = dtNow;

            strMoneyReqSeq = req.Insert(info);
            lblMoneyReqSeq.Text = strMoneyReqSeq;
            if (li3.Visible)
            {
                cmdProgress3_Click(null, null);
            }
            else
            {
                cmdProgress2_Click(null, null);
            }

            SetProgress(strMoneyReqSeq);
            Session["ReportSeq"] = strMoneyReqSeq;

            string strRefNo = info.ref_no;//ฝฝreq.GetInfo(strMoneyReqSeq).ref_no;
            lblRefNo.Text = strRefNo;

            if (strEventSeq == "16" && strBorrowReturnDate.Length > 0)
            {
                main.MessageAlert(Page, "รหัสรายการคือ [" + strRefNo + "] กำหนดชดใช้เงินยืมภายในวันที่ [" + strBorrowReturnDate + "]");
            }
            else
            {
                main.MessageAlert(Page, "รหัสรายการคือ [" + strRefNo + "]");
            }
        }

        protected void InsertNew()
        {
            string strMoneyReqSeq;
            string strJobsiteSeq = lblWorkingJobSiteSeq.Text;
            string strWorkAreaSeq = lblWorkAreaSeq.Text;
            string strMoneyReqDate = txtMoneyReqDate.GetStringValue();//lblMoneyReqDate.Text;
            string strBgmYear = txtBgmYear.Text;
            string strBgmSourceSeq = ddlBgmSourceSeq.SelectedValue;
            //string strBgmExpGrpSeq = rblBgmExpGrpSeq.SelectedValue;
            string strBgmProjOperSeq = ddlBgmProjOperSeq.SelectedValue;
            string strMoneyReqName = txtMoneyReqName.Text;
            string strEventSeq = rblEventSeq.SelectedValue;

            //string strRefNo = "";
            string strEmpSeq = Session["EmpSeq"].ToString();
            string strContactSeq = //(rblPayeeType.SelectedValue == "2" && strEventSeq == "12" ? null :ddlContactSeq.SelectedValue);
                rblPayeeType.SelectedValue == "2" ? ddlContactSeq.SelectedValue : null;
            string strCreditCardNo = null;

            string strBorrowNo = null;
            string strBorrowTypeSeq = null;
            string strBorrowStartDate = null;
            string strBorrowEndDate = null;
            string strBorrowReturnDate = null;
            string strDebtorEmpSeq = null;
            string strTransferTypeSeq = null;

            string strDocRefNo = null;

            string strUserID = strEmpSeq;
            //เช็คค่าว่าเป็นภาษีซื้อไม่ถึงกำหนดหรื่อไม่
            bool boolInvoice = false;

            DateTime dtNow = DateTime.Now;
            FIM_MoneyRequests.M_FIM_MoneyRequests info = new FIM_MoneyRequests.M_FIM_MoneyRequests();

            switch (strEventSeq)
            {
                case "16"://เงินยืม
                case "18"://ยืมบัตรเครดิต
                    //strBorrowNo = req.GenerateNextMoneyRequestBorrowNumber();
                    strBorrowTypeSeq = rblBorrowTypeSeq.SelectedValue;
                    strBorrowStartDate = dpBorrowStartDate.GetStringValue();
                    strBorrowEndDate = dpBorrowEndDate.GetStringValue();
                    strDebtorEmpSeq = strUserID;

                    CalReturnDate();
                    strBorrowReturnDate = lblBorrowReturnDate.Text;
                    strCreditCardNo = txtCreditCardNo.Text;
                    break;
                case "12"://เบิกเงิน
                case "17":
                    if (optListInvoice.SelectedValue == "")
                    {
                        boolInvoice = false;
                    }
                    else if (optListInvoice.SelectedValue == "0")
                    {
                        boolInvoice = false;
                    }
                    else if (optListInvoice.SelectedValue == "1")
                    {
                        boolInvoice = true;
                    }
                    strDocRefNo = txtDocRefNo.Text;
                    break;
                case "19":
                    strTransferTypeSeq = rblTransferTypeSeq.SelectedValue;
                    strBorrowStartDate = dpBorrowStartDate.GetStringValue();
                    strBorrowEndDate = dpBorrowEndDate.GetStringValue();

                    strBorrowReturnDate = lblBorrowReturnDate.Text;
                    info.transfer_job_site_seq = main.ConvertNullInt(ddlJobSiteSeq.SelectedValue);
                    break;
            }

            info.bgm_source_seq = main.ConvertNullInt(strBgmSourceSeq);
            info.bgm_year = main.ConvertNullInt(strBgmYear);
            info.work_area_seq = main.ConvertNullInt(strWorkAreaSeq);
            info.bgm_exp_grp_seq = main.ConvertNullInt(rblBgmExpGrpSeq.SelectedValue);
            info.event_seq = main.ConvertNullInt(strEventSeq);
            info.money_req_date = main.ConvertNullDateTime(strMoneyReqDate);
            info.use_advance = "0";
            info.advance_bgm_source_seq = null;
            string refNo = req.GenerateNextMoneyRequestRefNo(strBgmSourceSeq == "1" ? "1" : "2");
            if (string.IsNullOrEmpty(refNo))
            {
                if (string.IsNullOrEmpty(refNo = req.GenerateNextMoneyRequestRefNo(strBgmSourceSeq == "1" ? "1" : "2")))
                {
                    if (string.IsNullOrEmpty(refNo = req.GenerateNextMoneyRequestRefNo(strBgmSourceSeq == "1" ? "1" : "2")))
                    {
                        throw new Exception("Cannot generate refno (null)");
                    }
                }
            }
            info.ref_no = refNo;
            info.doc_ref_no = strDocRefNo;
            info.money_req_name = strMoneyReqName;
            info.money_req_name_plus = "";
            info.bgm_proj_oper_seq = main.ConvertNullInt(strBgmProjOperSeq);
            info.emp_seq = main.ConvertNullInt(strEmpSeq);
            info.job_site_seq = main.ConvertNullInt(strJobsiteSeq);
            info.contact_seq = main.ConvertNullInt(strContactSeq);
            info.icm_dn_seq = null;
            info.approver_seq = null;
            info.borrow_no = strBorrowNo;
            info.borrow_type_seq = main.ConvertNullInt(strBorrowTypeSeq);
            info.borrow_start_date = main.ConvertNullDateTime(strBorrowStartDate);
            info.borrow_end_date = main.ConvertNullDateTime(strBorrowEndDate);
            info.borrow_return_date = main.ConvertNullDateTime(strBorrowReturnDate);
            info.transfer_type_seq = main.ConvertNullInt(strTransferTypeSeq);
            info.allow = "0";
            info.debtor_emp_seq = main.ConvertNullInt(strDebtorEmpSeq);
            info.credit_card_no = strCreditCardNo;

            info.created_by = main.ConvertNullInt(strUserID);
            info.created_when = dtNow;
            info.chk_tax_invoice = boolInvoice;

            strMoneyReqSeq = req.Insert(info);
            lblMoneyReqSeq.Text = strMoneyReqSeq;
            if (li3.Visible)
            {
                cmdProgress3_Click(null, null);
            }
            else
            {
                cmdProgress2_Click(null, null);
            }

            SetProgress(strMoneyReqSeq);
            Session["ReportSeq"] = strMoneyReqSeq;

            string strRefNo = info.ref_no;//ฝฝreq.GetInfo(strMoneyReqSeq).ref_no;
            lblRefNo.Text = strRefNo;

            if (strEventSeq == "16" && strBorrowReturnDate.Length > 0)
            {
                main.MessageAlert(Page, "รหัสรายการคือ [" + strRefNo + "] กำหนดชดใช้เงินยืมภายในวันที่ [" + strBorrowReturnDate + "]");
            }
            else
            {
                main.MessageAlert(Page, "รหัสรายการคือ [" + strRefNo + "]");
            }
        }

        protected void Insert1()
        {
            if (!CheckData1())
                return;
            string strMoneyReqSeq;
            string strJobsiteSeq = lblWorkingJobSiteSeq.Text;
            string strWorkAreaSeq = lblWorkAreaSeq.Text;
            string strMoneyReqDate = txtMoneyReqDate.GetStringValue();//lblMoneyReqDate.Text;
            string strBgmYear = txtBgmYear.Text;
            string strBgmSourceSeq = ddlBgmSourceSeq.SelectedValue;
            //string strBgmExpGrpSeq = rblBgmExpGrpSeq.SelectedValue;
            string strBgmProjOperSeq = ddlBgmProjOperSeq.SelectedValue;
            string strMoneyReqName = txtMoneyReqName.Text;
            string strEventSeq = rblEventSeq.SelectedValue;
            //string strRefNo = "";
            string strEmpSeq = Session["EmpSeq"].ToString();
            string strContactSeq = //(rblPayeeType.SelectedValue == "2" && strEventSeq == "12" ? null :ddlContactSeq.SelectedValue);
                rblPayeeType.SelectedValue == "2" ? ddlContactSeq.SelectedValue : null;
            string strCreditCardNo = null;

            string strBorrowNo = null;
            string strBorrowTypeSeq = null;
            string strBorrowStartDate = null;
            string strBorrowEndDate = null;
            string strBorrowReturnDate = null;
            string strDebtorEmpSeq = null;
            string strTransferTypeSeq = null;

            string strDocRefNo = null;

            string strUserID = strEmpSeq;
            DateTime dtNow = DateTime.Now;
            FIM_MoneyRequests.M_FIM_MoneyRequests info = new FIM_MoneyRequests.M_FIM_MoneyRequests();

            switch (strEventSeq)
            {
                case "16"://เงินยืม
                case "18"://ยืมบัตรเครดิต
                    //strBorrowNo = req.GenerateNextMoneyRequestBorrowNumber();
                    strBorrowTypeSeq = rblBorrowTypeSeq.SelectedValue;
                    strBorrowStartDate = dpBorrowStartDate.GetStringValue();
                    strBorrowEndDate = dpBorrowEndDate.GetStringValue();
                    strDebtorEmpSeq = strUserID;

                    CalReturnDate();
                    strBorrowReturnDate = lblBorrowReturnDate.Text;
                    strCreditCardNo = txtCreditCardNo.Text;
                    break;
                case "12"://เบิกเงิน
                case "17":
                    strDocRefNo = txtDocRefNo.Text;
                    break;
                case "19":
                    strTransferTypeSeq = rblTransferTypeSeq.SelectedValue;
                    strBorrowStartDate = dpBorrowStartDate.GetStringValue();
                    strBorrowEndDate = dpBorrowEndDate.GetStringValue();

                    strBorrowReturnDate = lblBorrowReturnDate.Text;
                    info.transfer_job_site_seq = main.ConvertNullInt(ddlJobSiteSeq.SelectedValue);
                    break;
            }

            info.bgm_source_seq = main.ConvertNullInt(strBgmSourceSeq);
            info.bgm_year = main.ConvertNullInt(strBgmYear);
            info.work_area_seq = main.ConvertNullInt(strWorkAreaSeq);
            //info.bgm_exp_grp_seq = main.ConvertNullInt(strBgmExpGrpSeq);
            info.event_seq = main.ConvertNullInt(strEventSeq);
            info.money_req_date = main.ConvertNullDateTime(strMoneyReqDate);
            info.use_advance = "0";
            info.advance_bgm_source_seq = null;
            string refNo = req.GenerateNextMoneyRequestRefNo(strBgmSourceSeq == "1" ? "1" : "2");
            if (string.IsNullOrEmpty(refNo))
            {
                if (string.IsNullOrEmpty(refNo = req.GenerateNextMoneyRequestRefNo(strBgmSourceSeq == "1" ? "1" : "2")))
                {
                    if (string.IsNullOrEmpty(refNo = req.GenerateNextMoneyRequestRefNo(strBgmSourceSeq == "1" ? "1" : "2")))
                    {
                        throw new Exception("Cannot generate refno (null)");
                    }
                }
            }
            info.ref_no = refNo;
            info.doc_ref_no = strDocRefNo;
            info.money_req_name = strMoneyReqName;
            info.money_req_name_plus = "";
            info.bgm_proj_oper_seq = main.ConvertNullInt(strBgmProjOperSeq);
            info.emp_seq = main.ConvertNullInt(strEmpSeq);
            info.job_site_seq = main.ConvertNullInt(strJobsiteSeq);
            info.contact_seq = main.ConvertNullInt(strContactSeq);
            info.icm_dn_seq = null;
            info.approver_seq = null;
            info.borrow_no = strBorrowNo;
            info.borrow_type_seq = main.ConvertNullInt(strBorrowTypeSeq);
            info.borrow_start_date = main.ConvertNullDateTime(strBorrowStartDate);
            info.borrow_end_date = main.ConvertNullDateTime(strBorrowEndDate);
            info.borrow_return_date = main.ConvertNullDateTime(strBorrowReturnDate);
            info.transfer_type_seq = main.ConvertNullInt(strTransferTypeSeq);
            info.allow = "0";
            info.debtor_emp_seq = main.ConvertNullInt(strDebtorEmpSeq);
            info.credit_card_no = strCreditCardNo;

            info.created_by = main.ConvertNullInt(strUserID);
            info.created_when = dtNow;

            strMoneyReqSeq = req.Insert(info);
            lblMoneyReqSeq.Text = strMoneyReqSeq;
            if (li3.Visible)
            {
                cmdProgress3_Click(null, null);
            }
            else
            {
                cmdProgress2_Click(null, null);
            }

            SetProgress(strMoneyReqSeq);
            Session["ReportSeq"] = strMoneyReqSeq;

            string strRefNo = info.ref_no;//ฝฝreq.GetInfo(strMoneyReqSeq).ref_no;
            lblRefNo.Text = strRefNo;

            if (strEventSeq == "16" && strBorrowReturnDate.Length > 0)
            {
                main.MessageAlert(Page, "รหัสรายการคือ [" + strRefNo + "] กำหนดชดใช้เงินยืมภายในวันที่ [" + strBorrowReturnDate + "]");
            }
            else
            {
                main.MessageAlert(Page, "รหัสรายการคือ [" + strRefNo + "]");
            }
            /*
            else if (strEventSeq == "19")
            {

            }
            */
        }

        protected void Update1()
        {
            KPIS.GERP.ESS.APP.FIM_MoneyRequests.M_FIM_MoneyRequests currentMoneyRequest;
            string strMoneyReqSeq = lblMoneyReqSeq.Text;
            if (!CheckData1() || (currentMoneyRequest = req.GetInfo(strMoneyReqSeq)) == null)
                return;

            string strJobsiteSeq = lblWorkingJobSiteSeq.Text;
            string strWorkAreaSeq = lblWorkAreaSeq.Text;
            string strMoneyReqDate = txtMoneyReqDate.GetStringValue();//lblMoneyReqDate.Text;
            string strBgmYear = txtBgmYear.Text;
            string strBgmSourceSeq = ddlBgmSourceSeq.SelectedValue;
            string strBgmExpGrpSeq = rblBgmExpGrpSeq.SelectedValue;
            string strBgmProjOperSeq = ddlBgmProjOperSeq.SelectedValue;
            string strMoneyReqName = txtMoneyReqName.Text;
            string strEventSeq = rblEventSeq.SelectedValue;
            //string strRefNo = "";
            string strEmpSeq = Session["EmpSeq"].ToString();
            string strContactSeq = //(rblPayeeType.SelectedValue == "2" && strEventSeq == "12" ? null : ddlContactSeq.SelectedValue);
                rblPayeeType.SelectedValue == "2" ? ddlContactSeq.SelectedValue : null;

            string strBorrowNo = currentMoneyRequest.borrow_no;
            string strBorrowTypeSeq = null;
            string strBorrowStartDate = null;
            string strBorrowEndDate = null;
            string strBorrowReturnDate = null;
            string strDebtorEmpSeq = null;
            string strCreditCardNo = null;

            string strDocRefNo = null;
            string strTransferTypeSeq = null;

            string strUserID = strEmpSeq;
            DateTime dtNow = DateTime.Now;
            FIM_MoneyRequests.M_FIM_MoneyRequests info = new FIM_MoneyRequests.M_FIM_MoneyRequests();

            switch (strEventSeq)
            {
                case "16"://เงินยืม
                case "18"://เงินยืมบัตรเครดิต
                    strBorrowTypeSeq = rblBorrowTypeSeq.SelectedValue;
                    strBorrowStartDate = dpBorrowStartDate.GetStringValue();
                    strBorrowEndDate = dpBorrowEndDate.GetStringValue();
                    strDebtorEmpSeq = strUserID;

                    CalReturnDate();
                    strBorrowReturnDate = lblBorrowReturnDate.Text;
                    strCreditCardNo = txtCreditCardNo.Text;
                    break;
                case "12"://เบิกเงิน
                case "17":
                    strDocRefNo = txtDocRefNo.Text;
                    break;
                case "19":
                    strTransferTypeSeq = rblTransferTypeSeq.SelectedValue;
                    strBorrowStartDate = dpBorrowStartDate.GetStringValue();
                    strBorrowEndDate = dpBorrowEndDate.GetStringValue();

                    strBorrowReturnDate = lblBorrowReturnDate.Text;
                    info.transfer_job_site_seq = main.ConvertNullInt(ddlJobSiteSeq.SelectedValue);
                    info.transfer_type_seq = main.ConvertNullInt(strTransferTypeSeq);
                    break;
            }

            info.money_req_seq = Convert.ToInt32(strMoneyReqSeq);
            info.bgm_source_seq = main.ConvertNullInt(strBgmSourceSeq);
            info.bgm_year = main.ConvertNullInt(strBgmYear);
            info.work_area_seq = main.ConvertNullInt(strWorkAreaSeq);
            info.bgm_exp_grp_seq = main.ConvertNullInt(strBgmExpGrpSeq);
            info.event_seq = main.ConvertNullInt(strEventSeq);
            info.money_req_date = main.ConvertNullDateTime(strMoneyReqDate);
            //info.use_advance = "0";
            //info.advance_bgm_source_seq = null;
            info.ref_no = lblRefNo.Text;
            info.doc_ref_no = strDocRefNo;
            info.money_req_name = strMoneyReqName;
            info.money_req_name_plus = "";
            info.bgm_proj_oper_seq = main.ConvertNullInt(strBgmProjOperSeq);
            info.emp_seq = main.ConvertNullInt(strEmpSeq);
            info.job_site_seq = main.ConvertNullInt(strJobsiteSeq);
            info.contact_seq = main.ConvertNullInt(strContactSeq);
            //info.icm_dn_seq = null;
            //info.approver_seq = null;
            info.borrow_no = strBorrowNo;
            info.borrow_type_seq = main.ConvertNullInt(strBorrowTypeSeq);
            info.borrow_start_date = main.ConvertNullDateTime(strBorrowStartDate);
            info.borrow_end_date = main.ConvertNullDateTime(strBorrowEndDate);
            CalReturnDate();
            info.borrow_return_date = main.ConvertNullDateTime(strBorrowReturnDate);
            info.credit_card_no = strCreditCardNo;
            //info.allow = "0";
            info.debtor_emp_seq = main.ConvertNullInt(strDebtorEmpSeq);

            info.updated_by = main.ConvertNullInt(strUserID);
            info.updated_when = dtNow;

            req.Update(info);

            cmdProgress2_Click(null, null);
            Session["ReportSeq"] = strMoneyReqSeq;

            if (strEventSeq == "16")
            {
                main.MessageAlert(Page, "กำหนดชดใช้เงินยืมภายในวันที่ " + strBorrowReturnDate);
            }
        }

        protected void Delete1()
        {
            if (GrdViewAll.SelectedItems.Count > 0)
            {
                string strStatus = ((GridDataItem)GrdViewAll.SelectedItems[0])["record_status"].Text;
                if (strStatus == "W" || strStatus == "R")
                {
                    string strMoneyReqSeq = ((GridDataItem)GrdViewAll.SelectedItems[0])["money_req_seq"].Text;
                    string strUserID = Session["EmpSeq"].ToString();
                    DateTime dtNow = DateTime.Now;

                    FIM_MoneyRequests.M_FIM_MoneyRequests info = new FIM_MoneyRequests.M_FIM_MoneyRequests();
                    info.money_req_seq = Convert.ToInt32(strMoneyReqSeq);
                    info.updated_by = main.ConvertNullInt(strUserID);
                    info.updated_when = dtNow;

                    req.Delete(info);
                    LoadGrdViewAll();
                    SetControl("ViewAll");
                }
                else
                {
                    main.MessageAlert(Page, "ไม่สามารถลบรายการได้");
                }
            }
            else
            {
                main.MessageAlert(Page, "กรุณาเลือกรายการที่ต้องการลบ");
            }
        }

        protected void Insert3()
        {
            if (!CheckData3())
                return;

            string strMoneyReqSeq = lblMoneyReqSeq.Text;
            string strEmpSeq = ddlEmpSeq.SelectedValue;
            string strJobsiteSeq = lblJobsiteSeq.Text;
            string strClassSeq = lblClassSeq.Text;
            string strPosSeq = lblPosSeq.Text;
            string strUserID = Session["EmpSeq"].ToString();

            reqTrav.Insert(strMoneyReqSeq, strEmpSeq, null, null, strJobsiteSeq, strPosSeq, strClassSeq, null
                , "W", strUserID);
            LoadRepeaterPerson();
            ClearControl3();
            SetProgress(strMoneyReqSeq);
            cmdDelete1.Visible = false;
        }

        protected void Update3()
        {
            if (!CheckData3())
                return;

            string strTravelerSeq = lblTravelerSeq.Text;
            string strMoneyReqSeq = lblMoneyReqSeq.Text;
            string strEmpSeq = ddlEmpSeq.SelectedValue;
            string strJobsiteSeq = lblJobsiteSeq.Text;
            string strClassSeq = lblClassSeq.Text;
            string strPosSeq = lblPosSeq.Text;
            string strUserID = Session["EmpSeq"].ToString();

            reqTrav.Update(strTravelerSeq, strMoneyReqSeq, strEmpSeq, null, null
                , strJobsiteSeq, strPosSeq, strClassSeq, null, strUserID);
            LoadRepeaterPerson();
            ClearControl3();
            cmdDelete1.Visible = false;
        }

        protected void Delete3()
        {
            string strTravelerSeq = lblTravelerSeq.Text;
            string strUserID = Session["EmpSeq"].ToString();

            reqTrav.Delete(strTravelerSeq, strUserID);
            LoadRepeaterPerson();
            ClearControl3();
            SetProgress(lblMoneyReqSeq.Text);
            cmdDelete1.Visible = false;
        }

        protected void ExportToMsWord()
        {
            if (!PanelSummary.Visible)
            {
                main.MessageAlert(Page, "รองรับเฉพาะหน้าจอ [สรุปรายการ] เท่านั้น");
                return;
            }

            string strFileName = "Export-.doc";
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/vnd.ms-word";
            Response.ContentEncoding = System.Text.Encoding.UTF8;
            Response.AddHeader("Content-Disposition", "attachment;filename=" + strFileName);
            //EnableViewState = false;
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            PanelSummary.RenderControl(oHtmlTextWriter);
            Response.Output.Write(oStringWriter.ToString());
            Response.Flush();
            Response.End();
        }

        //==================== MessageSend ========================

        protected void LoadApprover()
        {
            //try
            //{
            // Edit by Aek 2014-07-29
            // Calculate base on to_emp_seq field in table message_send, not login session  
            DataTable dtb = send.GetCurrentApprover(SsMng.EmpSeq, null, "FIM_MONEY_REQUESTS", 1);
            ddlApprover.Enabled = true;

            if (dtb != null)
            {
                if (dtb.Rows.Count > 0)
                {
                    lblApprovePathSeq.Text = dtb.Rows[0]["approve_path_seq"].ToString();

                    if (dtb.Rows[0]["approver_type"].ToString() != "3")
                    {
                        PanelSendTo.Visible = true;
                        PanelSendTo2.Visible = false;
                        SetEmpInfo(dtb.Rows[0]["emp_seq"].ToString(), dtb.Rows[0]["pos_seq"].ToString());
                        ddlApprover.Enabled = false;
                    }
                    else
                    {
                        PanelSendTo.Visible = false;
                        PanelSendTo2.Visible = true;
                        lblPosGrpSeq.Text = dtb.Rows[0]["pos_grp_seq"].ToString();
                        lblPosGrpName.Text = dtb.Rows[0]["pos_grp_name"].ToString();
                        rptPosGrpDtls.DataSource = send.GetEmployeeInPositionGroup(dtb.Rows[0]["pos_grp_seq"].ToString());
                        rptPosGrpDtls.DataBind();
                    }
                }
            }
            //}
            //catch (Exception ex)
            //{
            //    send.WriteLog(ex.Message.Replace("'", "").Replace("\"", ""));
            //}
        }

        protected void ddlApprover_DataBound(object sender, EventArgs e)
        {
            //set the initial footer label
            ((Literal)ddlApprover.Footer.FindControl("RadComboItemsCount")).Text = Convert.ToString(ddlApprover.Items.Count);
        }

        protected void ddlApprover_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        {
            ddlApprover.DataSource = emplist.ListEmployeeByType("('1', '2', '3')", "");
            ddlApprover.DataBind();
        }

        protected void ddlEmpSeq_DataBound(object sender, EventArgs e)
        {
            //set the initial footer label
            ((Literal)ddlEmpSeq.Footer.FindControl("RadComboItemsCount")).Text = Convert.ToString(ddlEmpSeq.Items.Count);
        }

        protected void ddlEmpSeq_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
        {
            ddlEmpSeq.DataSource = emplist.ListEmployeeByType("('1', '2', '3')", "");
            ddlEmpSeq.DataBind();
        }

        protected void ddlApprover_ItemDataBound(object sender, RadComboBoxItemEventArgs e)
        {
            e.Item.Text = ((DataRowView)e.Item.DataItem)["full_name"].ToString();
            e.Item.Value = ((DataRowView)e.Item.DataItem)["sequence"].ToString();
        }

        protected void LoadDDLJobSiteSeq()
        {
            ddlJobSiteSeq.DataSource = jobsite.GetList("3");
            ddlJobSiteSeq.DataBind();

            main.InsertEmptyDDLSelect(ddlJobSiteSeq);
            ddlJobSiteSeq.SelectedIndex = 0;
        }

        protected void SetEmpInfo(string strEmpSeq, string strPosSeq)
        {
            if (strEmpSeq.Length > 0)
            {
                DataTable dtb = emplist.GetInfo(Convert.ToInt32(strEmpSeq));

                lblApproveEmpSeq.Text = dtb.Rows[0]["sequence"].ToString();
                lblApprovePosSeq.Text = string.IsNullOrEmpty(strPosSeq) ? dtb.Rows[0]["t_pos_seq"].ToString() : strPosSeq;
                ddlApprover.Text = dtb.Rows[0]["full_name"].ToString();
                lblAppPosName.Text = dtb.Rows[0]["pos_name"].ToString() + (strPosSeq != dtb.Rows[0]["t_pos_seq"].ToString() ? " (รักษาการณ์แทน " + GetPositionName(strPosSeq) + ")" : "");
                lblAppClassName.Text = dtb.Rows[0]["class_name"].ToString();
                lblAppExecPosName.Text = dtb.Rows[0]["exec_pos_name"].ToString();
                lblAppJobsiteName.Text = dtb.Rows[0]["job_site_name"].ToString();
            }
        }

        protected string GetPositionName(string strPosSeq)
        {
            dbManager.Open();
            DataTable dtb = null;

            try
            {
                dbManager.CreateParameters(1);
                dbManager.AddParameters(0, "@pos_seq", strPosSeq);

                string strSQL = "SELECT pos_name FROM hrm_mp_positions where pos_seq = @pos_seq";

                dtb = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0];
                if (dtb.Rows.Count > 0)
                {
                    return dtb.Rows[0][0].ToString();
                }

                return "";
            }
            catch
            {
                return "";
            }
            finally
            {
                dbManager.Dispose();
            }
        }

        protected void OnAjaxUpdate(object sender, ToolTipUpdateEventArgs args)
        {
            PanelMessageSend.Visible = true;
            args.UpdatePanel.ContentTemplateContainer.Controls.Add(PanelMessageSend);
        }

        protected void GrdViewAll_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                string strRecordStatus = ((GridDataItem)e.Item)["record_status"].Text;
                TableCell cell = ((GridDataItem)e.Item)["record_status_name"];

                switch (strRecordStatus)
                {
                    case "W":
                    default:
                        cell.ForeColor = System.Drawing.Color.Red;
                        break;
                    case "P":
                        cell.ForeColor = System.Drawing.Color.Blue;
                        break;
                    case "A":
                        cell.ForeColor = System.Drawing.Color.Green;
                        break;
                }
            }
        }

        protected void GrdViewAll_ItemCommand(object source, GridCommandEventArgs e)
        {
            if (e.CommandName == "Filter" || e.CommandName == "Sort" || e.CommandName == "Page")
            {
                LoadGrdViewAll();
            }
        }

        protected void rblEventSeq_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (rblEventSeq.SelectedValue)
            {
                case "16":
                case "18":
                    PanelBorrow.Visible = true;
                    PanelRequest.Visible = false;
                    //PanelRequest2.Visible = false;
                    PanelTransfer.Visible = false;
                    rblBorrowTypeSeq_SelectedIndexChanged(null, null);
                    cmdProgress3.Text = "ข้อมูลผู้เดินทาง";
                    ddlBgmProjOperSeq.Enabled = true;

                    if (rblEventSeq.SelectedValue == "18")
                    {
                        PanelCreditCardNo.Visible = true;
                        //ddlBgmSourceSeq.SelectedValue = "8";
                        //ddlBgmSourceSeq_SelectedIndexChanged(null, null);
                    }
                    else
                    {
                        PanelCreditCardNo.Visible = false;
                    }
                    //Add by Raksakaew on Feb 25,2014
                    cmdReportMoneyReqContracts.Visible = true;
                    cmdReportMoneyReq1.Visible = false;

                    break;
                case "12":
                    PanelBorrow.Visible = false;
                    PanelRequest.Visible = true;
                    PanelTransfer.Visible = false;
                    li3.Visible = false;
                    ddlBgmProjOperSeq.Enabled = true;
                    //Add by Raksakaew on Feb 25,2014
                    cmdReportMoneyReqContracts.Visible = false;
                    cmdReportMoneyReq1.Visible = true;
                    break;
                case "17":
                    PanelBorrow.Visible = false;
                    PanelRequest.Visible = true;
                    //PanelRequest2.Visible = false;
                    PanelTransfer.Visible = false;
                    li3.Visible = false;
                    ddlBgmSourceSeq.SelectedValue = "8";
                    ddlBgmSourceSeq_SelectedIndexChanged(null, null);
                    ddlBgmProjOperSeq.SelectedIndex = 0;
                    ddlBgmProjOperSeq.Enabled = false;
                    break;
                case "19":
                    PanelBorrow.Visible = false;
                    PanelRequest.Visible = false;
                    //PanelRequest2.Visible = false;
                    PanelTransfer.Visible = true;
                    rblTransferTypeSeq_SelectedIndexChanged(null, null);
                    cmdProgress3.Text = "ข้อมูลข้าราชการ";
                    ddlBgmProjOperSeq.Enabled = true;
                    break;
            }

            lblEventName.Text = rblEventSeq.SelectedItem.Text;
        }

        protected void rblBorrowTypeSeq_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (rblBorrowTypeSeq.SelectedValue.Length > 0)
            {
                string strBorrowTypeSeq = rblBorrowTypeSeq.SelectedValue;
                FIM_MAS_BorrowTypes.M_FIM_MAS_BorrowTypes info = borrowType.GetInfo(strBorrowTypeSeq);

                if (info.is_travel == "1")
                {
                    //cmdCalReturnDate.Enabled = true;
                    li3.Visible = true;
                }
                else
                {
                    //cmdCalReturnDate.Enabled = false;
                    li3.Visible = false;
                }
            }
            CalReturnDate();
        }

        protected void rblTransferTypeSeq_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strTransferTypeSeq = rblTransferTypeSeq.SelectedValue;

            if (strTransferTypeSeq == "1")
            {
                //cmdCalReturnDate.Enabled = true;
                li3.Visible = true;
            }
            else
            {
                //cmdCalReturnDate.Enabled = false;
                li3.Visible = false;
            }
        }

        protected void CalReturnDate()
        {
            DateTime? dtBorrowReturnDate = CalReturnDate(dpBorrowStartDate.GetDateValue(), dpBorrowEndDate.GetDateValue(), rblBorrowTypeSeq.SelectedValue);
            lblBorrowReturnDate.Text = dtBorrowReturnDate.HasValue ? dtBorrowReturnDate.Value.ToShortDateString() : "";
        }

        private DateTime? CalReturnDate(DateTime? dtBorrowStartDate, DateTime? dtBorrowEndDate, string strBorrowTypeSeq)
        {
            Session["strDayCount"] = "0";
            if (!dtBorrowStartDate.HasValue || !dtBorrowEndDate.HasValue || string.IsNullOrEmpty(strBorrowTypeSeq)) return null;

            DateTime? dtBorrowReturnDate = null;

            FIM_MAS_BorrowTypes.M_FIM_MAS_BorrowTypes info = borrowType.GetInfo(strBorrowTypeSeq);

            if (info.cal_date_type == "2")
            {
                dtBorrowReturnDate = dtBorrowEndDate.Value.AddDays(info.return_day.Value);
            }

            var tspDayCount = (dtBorrowEndDate.Value.AddDays(1) - dtBorrowStartDate);
            var strDayCount = tspDayCount.HasValue ? tspDayCount.Value.Days : 0;
            Session["strDayCount"] = strDayCount;

            return dtBorrowReturnDate;
        }

        protected void rblFilterEventSeq_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadGrdViewAll();

            //Add by Raksakaew on Feb 26,2014
            switch (rblFilterEventSeq.SelectedValue)
            {
                case "16":

                    cmdReportMoneyReqContracts.Visible = true;
                    lbtnContBorr.Visible = true;
                    cmdReportMoneyReq1.Visible = false;

                    break;
                case "12":

                    cmdReportMoneyReqContracts.Visible = false;
                    lbtnContBorr.Visible = false;
                    cmdReportMoneyReq1.Visible = true;

                    break;
            }
        }

        protected void rblBgmExpGrpSeq_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadGrdMoneyRequestDtls();
        }

        //protected void ddlBgmExpGrpSeqNew_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    //grdMoneyRequestDtlsNew.Visible = true;
        //    //PanelPayeeType.Visible = true;
        //    LoadGrdMoneyRequestDtlsNew();
        //}

        protected void ddlBgmSourceSeq_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            LoadDDLBgmProjOperSeq();
            LoadRblBorrowTypeSeq();
        }

        //protected void ddlBgmSourceSeq1_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        //{
        //    LoadDDLBgmProjOperSeq1();
        //    LoadGrdMoneyRequestDtlsNew();
        //}

        /*protected void rblIsBath_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool bIsBath = rblIsBath.SelectedValue == "1";
            PanelExchangeRate.Visible = !bIsBath;
            PanelBathAmount.Visible = !bIsBath;
        }

        protected void txtAmount2_TextChanged(object sender, EventArgs e)
        {
            if (rblIsBath.SelectedValue != "1")
            {
                CalculateBathAmount();
            }
            else
            {
                lblBathAmount.Text = txtAmount.Text;
            }
        }

        protected void txtExchangeRate_TextChanged(object sender, EventArgs e)
        {
            CalculateBathAmount();
        }

        protected void CalculateBathAmount()
        {
            double dAmount = (txtAmount.Text.Length > 0 ? Convert.ToDouble(txtAmount.Text) : 0);
            double dExchangeRate = (txtExchangeRate.Text.Length > 0 ? Convert.ToDouble(txtExchangeRate.Text) : 0);
            lblBathAmount.Text = (dAmount * dExchangeRate).ToString("#,###.00");
        }*/

        protected void ddlApprover_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            SetEmpInfo(ddlApprover.SelectedValue, null);
        }

        protected void grdMoneyRequestDtls_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            {
                bool bCanEdit = (lblRecordStatus.Text != "A" && lblRecordStatus.Text != "D");
                ((LinkButton)e.Item.FindControl("cmdAddExpense")).Enabled = bCanEdit;
            }

            //if (e.Item.ItemType == GridItemType.Item || e.Item.ItemType == GridItemType.AlternatingItem)
            //{
            //    bool bCanEdit = (lblRecordStatus1.Text != "A" && lblRecordStatus1.Text != "D");
            //    ((LinkButton)e.Item.FindControl("cmdAddExpense")).Enabled = bCanEdit;
            //}
        }

        protected void rblPayeeType_SelectedIndexChanged(object sender, EventArgs e)
        {
            PanelContact.Visible = rblPayeeType.SelectedValue == "2";
        }

        //protected void rblPayeeTypeNew_SelectedIndexChanged(object sender, EventArgs e)
        //{
        //    PanelContact.Visible = rblPayeeTypeNew.SelectedValue == "2";
        //}

        protected void txtBgmYear_TextChanged(object sender, EventArgs e)
        {
            LoadDDLBgmProjOperSeq();
        }

        //protected void txtBgmYear1_TextChanged(object sender, EventArgs e)
        //{
        //    LoadDDLBgmProjOperSeq();
        //    LoadGrdMoneyRequestDtlsNew();
        //}

        protected void ReturnDateAjaxRequest(object sender, AjaxRequestEventArgs e)
        {
            CalReturnDate();
        }

        protected void chkManual_CheckedChanged(object sender, EventArgs e)
        {
            txtAmount.Enabled = chkManual.Checked;
        }

        #endregion

        //protected void Page_Init(object sender, EventArgs e)
        //{
        //    if (GrdAddConAndMoney.Visible)
        //    {
        //        foreach (GridDataItem item in grdMoneyRequestDtlsNew.SelectedItems)
        //        {
        //            GridTemplateColumn template = new GridTemplateColumn();
        //            template.ItemTemplate = new ExpenseTemplate(main.WebString(item["m_bgm_projectoperationexpense.bgm_proj_oper_exp_seq"].Text));
        //            template.HeaderText = main.WebString(item["m_bgm_projectoperationexpense.m_bgm_projectexpense.m_bgm_mas_expense.bgm_exp_name"].Text);
        //            GrdAddConAndMoney.MasterTableView.Columns.Add(template);
        //        }
        //    }
        //}

        //protected void cmdConfirm_Click(object sender, EventArgs e)
        //{
        //    if (grdMoneyRequestDtlsNew.SelectedItems.Count > 0)
        //    {
        //        //repeatExpense.Visible = true;
        //        //repeatExpense.Items.
        //        GrdAddConAndMoney.Visible = true;
        //        txtBgmYear1.Enabled = false;
        //        ddlBgmSourceSeq1.Enabled = false;
        //        ddlBgmProjOperSeq1.Enabled = false;
        //        txtMoneyReqName1.Enabled = false;
        //        ddlBgmExpGrpSeqNew.Enabled = false;
        //        grdMoneyRequestDtlsNew.Enabled = false;

        //        foreach (GridDataItem item in grdMoneyRequestDtlsNew.SelectedItems)
        //        {
        //            GridTemplateColumn template = new GridTemplateColumn();
        //            template.ItemTemplate = new ExpenseTemplate(main.WebString(item["m_bgm_projectoperationexpense.bgm_proj_oper_exp_seq"].Text));
        //            template.HeaderText = main.WebString(item["m_bgm_projectoperationexpense.m_bgm_projectexpense.m_bgm_mas_expense.bgm_exp_name"].Text);
        //            GrdAddConAndMoney.MasterTableView.Columns.Add(template);
        //        }
        //        //Page_Init(null, null);
        //        //GrdAddConAndMoney.MasterTableView.InsertItem();
        //        //GrdAddConAndMoney.DataSource = new object();
        //        GrdAddConAndMoney.DataBind();
        //    }
        //    else
        //    {
        //        main.MessageAlert(Page, "ข้อมูลไม่ครบถ้วน กรุณากรอกข้อมูลให้ถูกต้อง");
        //    }
        //}

        //protected void BtContact_Click(object sender, EventArgs e)
        //{
        //    ShowCheckedItems(ddlContactSeq1, itemsClientSide);
        //    lblCheContact.Visible = true;
        //}

        //protected void RSContact_Click(object sender, EventArgs e)
        //{
        //    ShowCheckedItems(ComboBoxContactMoneyRequests, itemsClientSide);
        //    lblCheContact.Visible = true;
        //}


        #region Public Methods

        /*
        public void LoadDDLEmpSeq(RadComboBoxItemsRequestedEventArgs e)
        {
            string strFilter = e.Text;
            DataTable dtb = emplist.GetMoneyRequestTravelList(lblMoneyReqSeq.Text, strFilter).Tables[0];
            int nCount = dtb.Rows.Count;

            if (nCount > 0)
            {
                int itemOffset = e.NumberOfItems;
                int endOffset = Math.Min(itemOffset + 10, nCount);
                e.EndOfItems = endOffset == nCount;

                for (int i = itemOffset; i < endOffset; i++)
                {
                    ddlEmpSeq.Items.Add(new RadComboBoxItem(dtb.Rows[i]["emp_name"].ToString(), dtb.Rows[i]["emp_seq"].ToString()));
                }

                e.Message = main.GetStatusMessage(endOffset, nCount);
            }
        }
        */

        public void LoadData()
        {
            IList<M_FIM_MAS_Contacts> infolist = null;
            IBLL_FIM_MAS_Contacts con = new B_FIM_MAS_Contacts();

            infolist = GetListVender(null);

            ddlVender.DataSource = infolist;
            ddlVender.DataBind();

            main.InsertEmptyDDLSelect(ddlVender);
            ddlVender.SelectedIndex = 0;
        }

        public IList<M_FIM_MAS_Contacts> GetListVender(string strFilter)
        {
            System.Data.DataTable dt = new System.Data.DataTable();
            IList<M_FIM_MAS_Contacts> infoList = new List<M_FIM_MAS_Contacts>();
            string strConn = System.Web.Configuration.WebConfigurationManager.AppSettings["ConnectionString"];
            string qry = string.Format("SELECT * FROM IcmMasContact WHERE NameTh like '%{0}%' ORDER BY NameTh", strFilter);

            using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(strConn))
            {
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }

                using (System.Data.SqlClient.SqlDataAdapter adp = new System.Data.SqlClient.SqlDataAdapter(qry, con))
                {
                    adp.Fill(dt);
                }
            }

            foreach (DataRow row in dt.Rows)
            {
                M_FIM_MAS_Contacts info = new M_FIM_MAS_Contacts();

                info.contact_seq = int.Parse(row["ConId"].ToString());
                info.contact_name = row["TaxCode"] + " : " + row["NameTh"].ToString();

                infoList.Add(info);
            }

            return infoList;
        }

        #endregion

    }

    public class ExpenseTemplate : ITemplate
    {

        protected TextBox txtAmount;
        protected RadTextBox txtVat;
        protected RadTextBox txtVatAmount;
        protected RadTextBox txtTax;
        protected RadTextBox txtTaxAmount;
        private string colname;

        public ExpenseTemplate(string Name)
        {
            colname = Name;
        }

        public void InstantiateIn(System.Web.UI.Control container)
        {
            container.Controls.Add(new LiteralControl("test"));

            //txtAmount = new TextBox();
            //txtAmount.ID = "txtAmount";
            //container.Controls.Add(txtAmount);

            //txtVat = new RadTextBox();
            //txtVat.ID = "txtVat";
            //txtVat.Visible = true;
            //container.Controls.Add(txtVat);

            //txtVatAmount = new RadTextBox();
            //txtVatAmount.ID = "txtVatAmount";
            //txtVatAmount.Visible = true;
            //container.Controls.Add(txtVatAmount);

            //txtTax = new RadTextBox();
            //txtTax.ID = "txtTax";
            //txtTax.Visible = true;
            //container.Controls.Add(txtTax);

            //txtTaxAmount = new RadTextBox();
            //txtTaxAmount.ID = "txtTaxAmount";
            //txtTaxAmount.Visible = true;
            //container.Controls.Add(txtTaxAmount);
        }
    }

    public static class InsertGuidSessionManager
    {
        static InsertGuidSessionManager()
        {
            InsertGuidSessions = new List<InsertGuidSession>();
        }
        public static string GenerateNewGuid()
        {
            var newSession = new InsertGuidSession();
            newSession.Guid = Guid.NewGuid().ToString();
            newSession.GenerateWhen = DateTime.Now;
            InsertGuidSessions.Add(newSession);
            return newSession.Guid;
        }

        public static bool IsUse(string guid)
        {
            InsertGuidSession obj = InsertGuidSessions.FirstOrDefault(o => o.Guid == guid);
            if (obj == null)
            {
                throw new Exception("Use session not available");
            }
            else if (obj.UsedWhen.HasValue)
            {
                return false; // false = used, cant be use more
            }
            else
            {
                return true;
            }
        }

        public static bool UseGuid(string guid)
        {
            InsertGuidSession obj = InsertGuidSessions.FirstOrDefault(o => o.Guid == guid);
            if (obj == null)
            {
                throw new Exception("Use session not available");
            }
            else if (obj.UsedWhen.HasValue)
            {
                return false; // false = used, cant be use more
            }
            else
            {
                obj.UsedWhen = DateTime.Now;
                return true;
            }
        }
        public static List<InsertGuidSession> InsertGuidSessions { get; set; }
    }

    public class InsertGuidSession
    {
        public string Guid { get; set; }
        public DateTime GenerateWhen { get; set; }
        public DateTime? UsedWhen { get; set; }
    }
}



//public partial class NewTemplateColumn : GridTemplateColumn
//{
//    public void InstantiateIn(Control container)
//    {

//    }
//}

//public partial class TemplateColumn : ITemplate
//{
//    public void InstantiateIn(Control container)
//    {
//        KPIS.GERP.ESS.APP.FIM_MAS_Contacts.FIM_MAS_Contacts con = new KPIS.GERP.ESS.APP.FIM_MAS_Contacts.FIM_MAS_Contacts();
//        RadComboBox ComboContrac = new RadComboBox();
//        ComboContrac.ID = "ComboContrac";
//        ComboContrac.Visible = true;
//        ComboContrac.DataSource = con.GetList(null);
//        ComboContrac.DataBind();
//        container.Controls.Add(ComboContrac);
//    }
//}

